PHP If..Else
Programs become interesting when they can make decisions. PHP's if, else and elseif statements run a block of code only when a condition is true, and choose a different block otherwise.
The if statement
An if evaluates a condition in parentheses. When the result is truthy, the block in curly braces runs:
<?php
$temperature = 31;
if ($temperature > 30) {
echo "It's hot today. Drink some water!\n";
}
echo "Have a nice day.\n";It's hot today. Drink some water!
Have a nice day.
if...else
Add else for the case where the condition is false. Exactly one of the two blocks runs.
<?php
$stock = 0;
if ($stock > 0) {
echo "In stock: $stock items\n";
} else {
echo "Sold out\n";
}Sold out
if...elseif...else
To test several conditions in order, chain them with elseif. PHP checks each condition from top to bottom and runs only the first matching block. The final else catches everything else.
<?php
function grade(int $score): string {
if ($score >= 90) {
return "A";
} elseif ($score >= 80) {
return "B";
} elseif ($score >= 65) {
return "C";
} else {
return "F";
}
}
foreach ([95, 83, 70, 40] as $s) {
echo "$s => " . grade($s) . "\n";
}95 => A
83 => B
70 => C
40 => F
Because the checks run in order, a score of 95 never reaches the >= 80 test — it already matched the first one. Always put the most specific condition first.
Note: PHP accepts both
elseifandelse if. They behave the same with curly braces;elseifis the conventional spelling.
What counts as true?
A condition doesn't have to be a boolean. PHP converts the value to bool, and these values count as false ("falsy"):
| Falsy value | Example |
|---|---|
| boolean false | false |
| integer zero | 0 |
| float zero | 0.0 |
| empty string | "" |
| the string "0" | "0" |
| empty array | [] |
| null | null |
Everything else is truthy — including "false", " " (a space) and "0.0". The string "0" being falsy surprises many beginners.
<?php
$values = [0, "0", "", "0.0", " ", [], [0], null, "false"];
foreach ($values as $v) {
$shown = json_encode($v);
echo str_pad($shown, 8), $v ? "truthy" : "falsy", "\n";
}0 falsy
"0" falsy
"" falsy
"0.0" truthy
" " truthy
[] falsy
[0] truthy
null falsy
"false" truthy
Combining conditions
Use && (and), || (or) and ! (not) to build bigger conditions. Parentheses make the grouping clear:
<?php
$age = 16;
$withParent = true;
$isHoliday = false;
if ($age >= 18 || ($age >= 13 && $withParent)) {
echo "You may watch the movie.\n";
}
if (!$isHoliday) {
echo "The cinema is open at normal hours.\n";
}You may watch the movie.
The cinema is open at normal hours.
Short forms
For a simple "pick one of two values", the ternary operator is more compact than a full if:
$label = $count === 1 ? "item" : "items";When PHP is mixed with HTML templates, the alternative syntax with a colon and endif is easier to read than braces:
<?php if ($loggedIn): ?>
<p>Welcome back!</p>
<?php else: ?>
<a href="/login">Log in</a>
<?php endif; ?>Tip: Avoid deeply nested
ifblocks. Returning early from a function (if (!$valid) { return; }) usually makes code flatter and easier to follow.
Test yourself
Which of these values is falsy in PHP?
"false""0"" "[0]
In an if/elseif/else chain, how many blocks can run?
- All matching blocks
- At most one
- Exactly two
- It depends on
break
What closes an if (...): block in the alternative syntax?
end;fi;endif;}