This forum is now read-only. Please use our new forums at discuss.codecademy.com.

464 points
44feeb65ab99c6178db86ef37471b0b6?s=140&d=retro
Submitted by
ravi cho
about 6 years ago

What is wrong with my if/elseif/else statement?

Error message is that I am forgetting the else statement.

<html>
  <head>
    <title>If, Elseif, and Else</title>
  </head>
  <body>
    <p>
      <?php
        $guess = 7;
        $number = 7;

        if ($guess < $number) {
            echo "Too low!"
        } elseif ($guess > $number) {
            echo "Too high!"
        } else ($guess == $number) {
            echo "You win!"
        }

      ?>
    </p>
  </body>
</html>

Alex J edited this post to fix code formatting


1 vote

permalink

Try separating the second elseif into two words i.e. else if.
Also you are missing the semi-colons after your echos.
That would be my suggestion.

44 points
7f12e95943cc299aefee36b794f1bd15?s=140&d=retro
Submitted by
mmonitto
about 6 years ago

5 Comments

7f12e95943cc299aefee36b794f1bd15?s=140&d=retro mmonitto about 6 years ago

Never mind elseif does the exact same thing. I too am starting to learn PHP...

44feeb65ab99c6178db86ef37471b0b6?s=140&d=retro ravi cho about 6 years ago

darn

5207b520f10c60d05e003a00_975599717 MNorri1 about 6 years ago

The system accepted this as correct:

<?php
$guess = 7;
$number = 7;

if ($guess > $number) { print "Your guess os too high"; }
elseif ($guess < $number) {print "Your guess is too low";}
else{ print "You win!"; } ?>

  ?>

54d074fc9113cb1faa00074b_773763124 Peter Sahaidachny about 6 years ago

Why did you have to close your PHP code twice? '?> ?>'

5207b520f10c60d05e003a00_975599717 MNorri1 about 6 years ago

Peter, you right two PHP close tags are not needed.


1 vote

permalink

mmonitto wrote:

you are missing the semi-colons after your echos

not only that. There’s a bug in your else branch: nothing must ever go between else and the following { – or your code will produce a syntax error.

3117 points
Picture
Submitted by
Alex J
about 6 years ago


1 vote

permalink

Same error here. It gets me the message: "Parse error: syntax error, unexpected '{' on line 16"

<html>
  <head>
    <title>If, Elseif, and Else</title>
  </head>
  <body>
    <p>
      <?php
        $guess = 7;
        $number = 7;
        if ($guess < $number) {
        echo "Too low!";
        }
        elseif ($guess > $number) {
        echo "Too high!";
        }
        else ($guess == $number) {
        echo "You win!";
        }
      ?>
    </p>
  </body>
</html>

195 points
Ecd8a5fb03ca2d9cc17186d057b2437e?s=140&d=retro
Submitted by
Marian Cimbru
about 6 years ago

2 Comments

6583e68bf4143718fdb6cca42afb9218?s=140&d=retro level45 about 6 years ago

Omit your ($guess == $number ) from the else. Then you will "Win!"

51d09c9a8c1cccf0540050ed_259136998 Pcat about 6 years ago

thank you level45! That was it!


0 votes

permalink

My complete code is this:

If, Elseif, and Else



<?php
$guess = 7;
$number = 7;

    // Write your if/elseif/else statement here!
    if ($guess < $number) {
        echo "Too low!";
    }

    elseif ($guess > $number) {
        echo "Too high!";
    }


    else ($number == $guess) {
        echo "You win!";
    }

  ?>
</p>

And I'm getting the error message: "Parse error: syntax error, unexpected '{' on line 21"
Any help?! Thanks

214 points
B23a66e3cfb7ecb7715b5bbb6cf958f6?s=140&d=retro
Submitted by
ScottCode3683
almost 6 years ago


0 votes

permalink

you need semi colons!!!!!!!!!!!!!!!!!

160 points
4f10048b14153b9d5729d263566986eb?s=140&d=retro
Submitted by
al5do028
almost 6 years ago


0 votes

permalink

Can someone explain why the else statement generates a syntax error when you make an equals statement like the examples above?

I would think the parser would recognize a true statement, but the code only works when omitting a conditional statement.

284 points
4af95f61315632587ecbc878047544cf?s=140&d=retro
Submitted by
c130ldmaster
almost 6 years ago