Originally published on zero2php.dev — part of the Zero to PHP course. View this lesson on the original site.

Why branching matters

Branching lets programs react to data.

Example

<?php

declare(strict_types=1);

$score = 85;

if ($score >= 90) {
    echo "A\n";
} elseif ($score >= 80) {
    echo "B\n";
} else {
    echo "Other\n";
}

Rules of thumb

Exactly one branch runs in this if / elseif / else chain (the first that matches).

Keep conditions readable—if a condition is hard, split into boolean variables with clear names.

Exercise

Let $score = 72. Print high if $score >= 90, else mid if $score >= 60, else low—expected output mid.

From Zero to PHP — Control flow, functions, arrays, and the web