Control Flow
PHP's if/elseif/else reads almost exactly like C, Java, or JavaScript — parentheses around the condition, braces around each branch, and elseif written as one word (else if with a space also works, but elseif is the idiom you'll see most often in PHP code).
switch compares one value against a series of case labels using loose equality (==, with all its type-juggling behavior from the previous lesson) and, like C, falls through to the next case unless you break out of it explicitly.
<?php
$score = 72;
if ($score >= 90) {
echo "A\n";
} elseif ($score >= 70) {
echo "B\n";
} else {
echo "C\n";
}
$grade = "B";
switch ($grade) {
case "A":
echo "Excellent\n";
break;
case "B":
echo "Good\n";
break;
default:
echo "Keep trying\n";
}
B Good
$score of 72 is not >= 90, so the first condition's branch is skipped; it is >= 70, so "B" prints and the else branch never runs. The switch then matches $grade against "B" exactly, prints "Good", and break stops it from also running the default case. Next: loops — for, while, and foreach, including foreach over an associative array.
Practice exercise
Given $score = 85, use if/elseif/else to echo "A" if $score is at least 90, "B" if at least 80, otherwise "C".