← Back to PHP Course | Chapter 4: Control Flow | Lesson 6 of 14

PHP elseif Ladder

An elseif ladder is like a row of questions asked one by one: are you under 5? under 12? under 18? PHP stops at the first yes and ignores the rest.
Syntax
php
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
<?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";
}
?>

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

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

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

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
<?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";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing else if instead of elseif in colon syntax, which causes a parse error.
  2. Putting a broad condition like $score >= 50 before $score >= 90, so the later, more specific branch is never reached.
  3. Using separate if statements instead of elseif, so several branches can run when only one should.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.