This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by ravi cho
over 10 years

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

Answer 51fc5ac180ff33ad6400082d

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.

points
Submitted by mmonitto
over 10 years

5 comments

mmonitto over 10 years

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

ravi cho over 10 years

darn

MNorri1 over 10 years

The system accepted this as correct:

Pete Sahaidachny over 10 years

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

MNorri1 over 10 years

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

Answer 51ff9bd1abf8215685006b6f

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.

points
Submitted by Alex J
over 10 years

Answer 520a364180ff3399280021f4

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>
points
Submitted by Marian Cimbru
over 10 years

2 comments

level45 over 10 years

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

Pcat over 10 years

thank you level45! That was it!

Answer 521870d2abf821a58d001ad8

0 votes

Permalink

My complete code is this:

points
Submitted by ScottCode3683
over 10 years

Answer 521b7278f10c605bc4002584

0 votes

Permalink

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

points
Submitted by al5do028
over 10 years

Answer 5224abbaf10c60ddb40026c1

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.

points
Submitted by c130ldmaster
over 10 years