为什么我会为简单的if和switch语句得到解析错误

时间:2017-07-20 02:52:26

标签: php

有谁能告诉我为什么我会为这些简单的代码得到解析错误? 我使用的PHP版本是5.5.12。

<?php
$favcolor = "red";

switch ($favcolor) {
    case "red":
        echo "Your favorite color is red!";
        break;
    case "blue":
        echo "Your favorite color is blue!";
        break;
    case "green":
        echo "Your favorite color is green!";
        break;
    default:
        echo "Your favorite color is neither red, blue, nor green!";

}

?>

(!)解析错误:第5行的语法错误,意外的'Â'(T_STRING),期待案例(T_CASE)或默认(T_DEFAULT)或'}'

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$email = $gender = $comment = $website = "";

$MyVar = $_SERVER["REQUEST_METHOD"];
echo $_SERVER["REQUEST_METHOD"];

if ($MyVar == "GET") {
    echo "This is it.\n";

}


}

?>

(!)解析错误:语法错误,第18行意外的'{'

1 个答案:

答案 0 :(得分:0)

第一个代码是完全正确的!

第二个,你已经增加了额外的&#39;}&#39;在文件的最后。只需使用:

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$email = $gender = $comment = $website = "";

$MyVar = $_SERVER["REQUEST_METHOD"];
echo $_SERVER["REQUEST_METHOD"];

if ($MyVar == "GET") {
    echo "This is it.\n";
}

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$email = $gender = $comment = $website = "";

$MyVar = $_SERVER["REQUEST_METHOD"];
echo $_SERVER["REQUEST_METHOD"];

if ($MyVar == "GET")
    echo "This is it.\n";