if and else
A branch decides which lines of a PHP program run. The if statement is the first of them, and the shape it takes decides how flat the function stays.
The if statement
if runs its block only when the condition holds. Anything that becomes true or false works as a condition:
<?php $score = 75; if ($score >= 60) { echo "Passed!", PHP_EOL; }
php if.php
Passed!
Most non-empty strings are true in a condition, but "0" is the one that is false:
<?php if ("0") { echo "true", PHP_EOL; } else { echo "false", PHP_EOL; }
php zero.php
false
The string holds one character and is still false, because PHP reads "0" as zero when it asks a value for its truth. Write $name !== "" when an empty string is the only thing to reject.
elseif chains
elseif adds more branches, else catches everything left. Only the first matching branch runs:
<?php $score = 85; if ($score >= 90) { echo "Excellent", PHP_EOL; } elseif ($score >= 60) { echo "Passed", PHP_EOL; } else { echo "Failed", PHP_EOL; }
php elseif.php
Passed
The first condition is false, the second is true, so the chain stops there and the else never runs. A score of 95 would take the first branch, and a score of 40 the third.
Nesting vs early return
Nested branches pile indentation and hide the main path. Returning early keeps the happy path flat:
<?php function label(int $score): string { if ($score < 60) { return "Failed"; } return "Passed"; } echo label(75), PHP_EOL; echo label(42), PHP_EOL;
php label.php
Passed
Failed
The rejection leaves the function at once, so the last return is reached only by a score that passed, and no else is needed.
Guard clauses
Guard clauses push every rejection to the top of the function. What remains below is the real work, free of else blocks:
<?php function admit(?string $name, int $age): string { if ($name === null) { return "No name given"; } if ($age < 18) { return "Too young"; } return "Welcome, " . $name . "!"; } echo admit(null, 20), PHP_EOL; echo admit("Ada", 16), PHP_EOL; echo admit("Ada", 20), PHP_EOL;
php admit.php
No name given
Too young
Welcome, Ada!
The three calls show the three exits: the first guard, the second guard, then the welcome line. The ?string type lets the name be null, which is why the first guard can compare it with null at all.
Exercises
- Change $score in if.php to 59 and run it again. What does the program print now, and why?
- Add a branch for a score of exactly 100 to the elseif chain and check that only one line still prints.
- Rewrite label() with an else instead of the early return. Which version is easier to read, and why?
- Give admit() a third guard: a name shorter than two characters is rejected with "Name too short". Call it with "A".
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
When does the
elsebranch of anifrun?Answer
When every earlier condition of the chain was false
The else is the last resort of the chain: it runs when no earlier condition held.
-
How many branches of an
if/elseif/elsechain can run in one pass?Answer
Exactly one
The chain stops at the first matching branch, so at most one of its branches runs.
-
What is the point of an early return?
Answer
It leaves the function at once, so the rest needs no else
A return ends the function call, so a rejection can leave the function before the main work starts.
-
Where does a guard clause belong?
Answer
At the top of the function, before the work
A guard rejects the input first, so everything below it can assume the input is usable.
-
What does this program print?
<?php if (0) { echo "yes"; } else { echo "no"; }Answer
no
The integer 0 is false in a condition, so the else branch runs.
-
What does this program print?
<?php if ("0") { echo "true"; } else { echo "false"; }Answer
false
The string "0" is the one non-empty string PHP treats as false, so the else branch runs.
-
What does this program print?
<?php $n = 10; if ($n > 5) { echo "big"; } elseif ($n > 0) { echo "small"; }Answer
big
The first condition already holds, so the chain stops and the elseif is never tested.
-
What does this program print?
<?php function label(int $score): string { if ($score < 60) { return "Failed"; } return "Passed"; } echo label(42);Answer
Failed
The score is below 60, so the guard returns "Failed" before the last line is reached.
-
What does this program print?
<?php function admit(?string $name, int $age): string { if ($name === null) { return "No name given"; } return "Welcome, " . $name . "!"; } echo admit(null, 20);Answer
No name given
The nullable type accepts null, and the first guard returns the message before the welcome line.
-
What does this program print?
<?php if (true) { echo "a"; } if (true) { echo "b"; }Answer
ab
Two separate if statements are not a chain: both conditions are tested and both blocks run.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev