if, elseif and else
What you'll learn:
- choose which lines a program runs with
if,elseif,elseand the ternary operator; - predict which values PHP treats as false and tell
isset,emptyandis_nullapart; - explain how
switchcompares values and why the nextcaseruns withoutbreak; - choose between
switchandmatchand explain the difference in an interview.

A program without conditions runs every line in order, from top to bottom. But a real program often has to choose: show a discount or the regular price, greet someone by name or ask them to introduce themselves.
Picture a fork in a road with a signpost on it. The sign says to go left to the bus if it's raining, and otherwise to go right and walk. A traveler reads the sign, looks at the sky and takes one of the two roads. In PHP, that signpost is the if statement.
Let's look at a program that chooses what to print:
<?php
$temperature = 25;
if ($temperature > 20) {
echo 'warm', "\n"; // warm
} else {
echo 'cold', "\n";
}
echo 'done', "\n"; // doneLet's go through it step by step:
$temperature = 25;puts the number25into the variable.if ($temperature > 20)evaluates the expression in parentheses:25 > 20givestrue.- Since the result is
true, PHP runs the lines in the first pair of braces and printswarm. - PHP skips the lines after
else. echo 'done'comes after the whole construct and runs either way.
Change 25 to 15: the comparison gives false, PHP skips the first braces, runs the lines after else and prints cold, then done.
The condition and the branches
A condition is the expression in parentheses after if, which PHP evaluates to true or false. Most often it is a comparison (>, <=, ===) or several comparisons joined with && and ||.
The lines in braces are called a branch. What matters about the if and else branches:
- the
ifbranch runs when the condition is true; - the
elsebranch runs when it is false; elseis optional: without it, nothing runs when the condition is false;- exactly one of the two branches runs, never both;
- the braces are always written, even around a single line.
The elseif chain
When there are more than two roads, you add elseif branches—"else, if"—between if and else. Let's take apart a program that turns a score into a grade:
$score = 85;
if ($score >= 90) {
echo 'A', "\n";
} elseif ($score >= 75) {
echo 'B', "\n"; // B
} elseif ($score >= 50) {
echo 'C', "\n";
} else {
echo 'F', "\n";
}Let's go through it step by step:
85 >= 90is false, so PHP skips theAbranch.85 >= 75is true, so PHP printsB.- PHP looks no further: the condition
85 >= 50is true as well, but it is never even checked, andelsedoes not run.
The rules of the chain:
- the conditions are checked from top to bottom, one at a time;
- the first branch whose condition is true runs, and only that one;
- the remaining branches are skipped, even if their conditions are true too;
- the
elseat the end runs only when no condition fits.
That is why the order of the conditions matters: the narrowest one goes first. Move $score >= 50 to the very top, and the program prints C for 85 and for 99 points alike.
This is where the signpost analogy stops working. A traveler sees every road at once and picks the best one, while PHP reads the conditions one at a time and turns onto the first road that fits.
elseif is written as one word. PHP also understands else if in two words, but PHP code uses elseif.
A branch can hold another if—a nested condition. For example, if ($age >= 18) { if ($hasTicket) { … } } checks the ticket of adults only. Two levels of nesting read fine, but deeper code tangles quickly. A nested if is often replaced with one condition: if ($age >= 18 && $hasTicket).
The ternary operator
Sometimes the branches exist only to pick one of two values. That is what the ternary operator is for: $condition ? $a : $b gives $a when the condition is true and $b when it is false.
$age = 20;
$ticket = $age >= 18 ? 'adult' : 'child';
echo $ticket, "\n"; // adult
echo $age >= 65 ? 'senior' : 'regular', "\n"; // regularLet's go through it step by step:
$age >= 18is true, so the operator gives'adult', and that value goes into$ticket.- In the second line the operator sits right inside
echo:20 >= 65is false, andregularis printed.
The ternary operator is an expression: it has a value that you can assign to a variable or pass to echo. An if has no value: it only chooses which lines to run.
As you already know, the operator has a short form, $a ?: $b, without the middle part. PHP 8, however, does not accept two ternary operators in a row without parentheses: a file with $a ? $b : $c ? $d : $e does not run at all. For three options or more, use an elseif chain.
Common mistake
The braces were left out, and the second line of the branch ended up outside it:
$age = 15;
if ($age >= 18)
echo 'adult', "\n";
echo 'can vote', "\n"; // ✗PHP lets you write if without braces, but then only the next single statement belongs to the condition. Indentation means nothing, so the program prints can vote even for a fifteen-year-old. There is no error message. The fix is braces around both lines, which is why PHP code always has them.
Key points
ifruns its branch when the condition is true,elsewhen it is false; the braces are always written.- In an
elseifchain only the first branch with a true condition runs, so the narrowest condition goes first. - The ternary operator
$condition ? $a : $bis an expression that gives one of two values.