Conditionals in PHP
PHP's core conditional construct is if / elseif / else, evaluated top to bottom until a branch's condition is truthy or the else fallback is reached. PHP's truthiness rules matter here: 0, 0.0, "", "0", empty arrays, and null are all falsy, while every other value (including "0.0" and " " — a space) is truthy. This differs subtly from languages like JavaScript, so it is worth memorizing PHP's specific falsy list rather than assuming parity.
Cricket analogy: PHP evaluating if/elseif/else top to bottom until one condition is truthy is like an umpire running through a decision checklist top to bottom until one applies (no-ball, then wide, then normal delivery); remembering that '0' as a string is truthy in PHP is like remembering a specific ICC rule that trips up commentators used to other sports.
Ternary and Null Coalescing Shortcuts
The ternary operator (condition ? valueIfTrue : valueIfFalse) offers a compact one-line conditional expression. PHP also supports a shorthand ternary, $a ?: $b, which returns $a if it is truthy, otherwise $b — useful for defaults, though ?? (null coalescing) is generally preferable when you specifically care about null/undefined rather than general falsiness, since ?: still treats 0 or an empty string as needing the fallback.
Cricket analogy: The ternary $isOut ? 'Out' : 'Not Out' gives a compact one-line verdict, while the shorthand $review ?: 'Umpire\'s Call' falls back only when $review is falsy; but if a batter's runs is legitimately 0, ?? (null coalescing) is safer than ?: since ?: would wrongly treat a real score of 0 as needing a fallback.
switch and the Alternative Syntax
switch compares a value against multiple case labels using loose equality (==) by default, and cases fall through unless explicitly terminated with break — a frequent source of bugs, which is one reason match (PHP 8.0+) is now preferred for value-based branching. For templates mixing PHP with HTML, PHP offers an alternative syntax using colons and endif/endif/endforeach instead of braces, improving readability when control structures span large blocks of markup.
Cricket analogy: switch comparing a delivery type against case labels with loose == and falling through without break is like a scorer forgetting to log 'wide' separately and letting it bleed into the next ball's tally; match (PHP 8+) with strict comparison is the modern, safer scoring method preferred for exact delivery classification.
<?php
declare(strict_types=1);
function classify(int $score): string {
if ($score >= 90) {
$grade = 'A';
} elseif ($score >= 80) {
$grade = 'B';
} elseif ($score >= 70) {
$grade = 'C';
} else {
$grade = 'F';
}
return $grade;
}
$score = 84;
echo classify($score) . PHP_EOL;
$label = $score >= 60 ? 'Pass' : 'Fail';
echo $label . PHP_EOL;
?>
<ul>
<?php foreach ([90, 72, 55] as $s): ?>
<li><?= classify($s) ?></li>
<?php endforeach; ?>
</ul>
The alternative colon syntax (if: ... endif;) exists primarily for template files where PHP is interleaved with large amounts of HTML — it avoids a wall of nested closing braces and makes it visually obvious where a control block ends, even many lines later.
switch uses loose comparison by default, so case "0": can match an integer 0 or even certain unrelated values due to type juggling. Combined with fallthrough (missing break), this makes switch error-prone for anything beyond simple, well-understood cases — many teams now prefer match for value comparisons.
- if/elseif/else evaluates branches top-down; PHP's falsy values include 0, "", "0", null, empty array, and false.
- Ternary (?:) offers a compact conditional expression; its shorthand form (?:) tests truthiness, not just null.
- ?? specifically checks for null/undefined and is safer than ?: when 0 or "" are valid values.
- switch uses loose (==) comparison by default and falls through cases without an explicit break.
- match (PHP 8+) uses strict comparison and has no fallthrough, making it a safer alternative to switch.
- The colon-based alternative syntax (if: ... endif;) improves readability in HTML-heavy template files.
Practice what you learned
1. Which of these values is truthy in PHP?
2. What is the key difference between ?? and the shorthand ternary ?: ?
3. What comparison operator does switch use by default when matching case values?
4. What happens if a switch case is missing a break statement?
5. What is the primary use case for PHP's alternative colon syntax (if: ... endif;)?
Was this page helpful?
You May Also Like
Match Expressions
Introduces PHP 8's match expression, its strict comparison semantics, and why it is often a safer, more concise replacement for switch.
Operators and Expressions
Explains PHP's arithmetic, comparison, logical, assignment, and null-coalescing operators, along with operator precedence and expression evaluation order.
Loops in PHP
Explains PHP's for, while, do-while, and foreach loops, along with break, continue, and reference-based iteration pitfalls.
PHP Syntax and Variables
Covers PHP's basic script structure, statement termination, comments, and how variables are declared, scoped, and named in PHP 8.x.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics