This forum is now read-only. Please use our new forums! Go to forums
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
Answer 51ff9bd1abf8215685006b6f
Answer 520a364180ff3399280021f4
1 vote
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>
Answer 521870d2abf821a58d001ad8
Answer 521b7278f10c605bc4002584
Answer 5224abbaf10c60ddb40026c1
0 votes
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.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
5 comments
Never mind elseif does the exact same thing. I too am starting to learn PHP…
darn
The system accepted this as correct:
Why did you have to close your PHP code twice? ‘?> ?>’
Peter, you right two PHP close tags are not needed.