PHP elseif Ladder
In this page:
if (condition1) {
// ...
} elseif (condition2) {
// ...
} else {
// none matched
}
Basic elseif Structure
An elseif ladder tests a series of conditions from top to bottom and runs the block belonging to the *first* one that evaluates to true, then skips every condition and block after it — even if a later condition would also have matched.
Example: Basic elseif Structure
<?php
// Declare `$score`, set to `85`
$score = 85;
// Check whether `$score >= 90`
if ($score >= 90) {
// Print "A" to the output
echo "A";
// Otherwise, if `$score >= 80`, run this branch
} elseif ($score >= 80) {
// Print "B" to the output
echo "B";
// Otherwise, run this branch
} else {
// Print "C" to the output
echo "C";
}
?>
Login to try C/C++/Java/PHP code in the editor
Multiple elseif Blocks
You can chain as many elseif blocks as the logic needs, which makes this the natural structure when a value needs to be sorted into one of several distinct categories or ranges, rather than just a simple true/false split.
Example: Multiple elseif Blocks
<?php
// Declare `$grade`, set to `72`
$grade = 72;
// Check whether `$grade >= 90`
if ($grade >= 90) {
// Print "A" to the output
echo "A";
// Otherwise, if `$grade >= 80`, run this branch
} elseif ($grade >= 80) {
// Print "B" to the output
echo "B";
// Otherwise, if `$grade >= 70`, run this branch
} elseif ($grade >= 70) {
// Print "C" to the output
echo "C";
// Otherwise, if `$grade >= 60`, run this branch
} elseif ($grade >= 60) {
// Print "D" to the output
echo "D";
// Otherwise, run this branch
} else {
echo "F";
}
?>
Login to try C/C++/Java/PHP code in the editor
Ordering Conditions
Because PHP stops at the first true condition, order matters: placing a broad, easily-satisfied condition near the top of the ladder can silently steal cases that were meant to reach a more specific condition further down — always order from most specific to most general.
Example: Ordering Conditions
<?php
// Declare `$age`, set to `25`
$age = 25;
// Check whether `$age >= 0`
if ($age >= 0) {
// Print "Matched too broad first condition" to the output
echo "Matched too broad first condition";
// Otherwise, if `$age >= 18`, run this branch
} elseif ($age >= 18) {
// Print "This never runs -- unreachable" to the output
echo "This never runs -- unreachable";
}
?>
Login to try C/C++/Java/PHP code in the editor
Alternative Colon Syntax
The alternative colon syntax works the same way here as with plain if/else, but PHP requires elseif to be written as one word (not else if) when using this style — a small but easy-to-miss syntax rule specific to the colon form.
Example: Alternative Colon Syntax
<?php
// Declare `$role`, set to "editor"
$role = "editor";
// Check whether `$role === "admin"`
if ($role === "admin"):
// Print "Admin access" to the output
echo "Admin access";
// Otherwise, if `$role === "editor"`, run this branch
elseif ($role === "editor"):
// Print "Editor access" to the output
echo "Editor access";
// Otherwise, run this branch
else:
// Print "No access" to the output
echo "No access";
endif;
?>
Login to try C/C++/Java/PHP code in the editor
Default Fallback Else
A final else with no condition attached catches every case none of the preceding branches matched. It only ever runs if the entire ladder above it fell through, making it the safety net for values you didn't explicitly plan for.
Example: Default Fallback Else
<?php
// Declare `$day`, set to "Sunday"
$day = "Sunday";
// Check whether `$day === "Saturday"`
if ($day === "Saturday") {
// Print "Weekend" to the output
echo "Weekend";
// Otherwise, if `$day === "Monday"`, run this branch
} elseif ($day === "Monday") {
// Print "Start of week" to the output
echo "Start of week";
// Otherwise, run this branch
} else {
// Print "Some other day" to the output
echo "Some other day";
}
?>
Login to try C/C++/Java/PHP code in the editor
- Writing
else ifinstead ofelseifin colon syntax, which causes a parse error. - Putting a broad condition like
$score >= 50before$score >= 90, so the later, more specific branch is never reached. - Using separate
ifstatements instead ofelseif, so several branches can run when only one should.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: