1

Can anyone tell me why I am getting parse errors for these simple codes? The PHP version I am using is 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!";

}

?>

( ! ) Parse error: syntax error, unexpected '   ' (T_STRING), expecting case (T_CASE) or default (T_DEFAULT) or '}' in on line 5

<?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";

}


}

?>

( ! ) Parse error: syntax error, unexpected '{' in on line 18

  • 3
    Please, tell me what is your text editor and the encoding used? – user10089632 Jul 20 '17 at 02:53
  • the first code seems working fine and on the second one, you have an extra closing `}` at the end. – ASR Jul 20 '17 at 03:07
  • 1
    The only person making any real sense here so far is @Lambda7 Yes there's an extra closing bracket, but that's not even the error. The error is an unexpected _opening_ bracket on line 18 while the code has 17 lines. Clearly we're missing something here and OP should clerify his question including _all_ the relevant code. – icecub Jul 20 '17 at 03:14
  • What we can't see is if the double quotes are regular old quotes or if they're actually the fancy open and close quotes like word or outlook uses. Same for the curly braces. I've never tried it, but I bet that would give errors like this. IOW, what @Lambda7 said. – Tim Morton Jul 20 '17 at 03:50

1 Answers1

0

The first code is totally right!

The second one, you have put an extra '}' in the end of the file. just use:

<?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";
}

or

<?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";