100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
PHP

Conditionals in PHP

Covers if/elseif/else, the ternary and null-coalescing shortcuts, switch statements, and alternative control-structure syntax used in templates.

Control FlowBeginner7 min readJul 9, 2026
Analogies

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

Was this page helpful?

Topics covered

#PHP#PHPProgrammingStudyNotes#Programming#ConditionalsInPHP#Conditionals#Ternary#Null#Coalescing#StudyNotes#SkillVeris