control
$size = 10;
$bucket = match (true) {
$size > 100 => "big",
$size > 4 => "small",
default => "tiny",
};
Output:
small
match
match returns a value
It uses strict comparison
No fall through, so the classic missing break bug disappears
if
Use if when the condition has side effects or several steps
Use match when one expression maps to one result
loops
$total = 0;
foreach ([1, 2, 3] as $n) {
$total += $n;
}
foreach is the normal loop for arrays
while fits when the number of iterations is unknown
Next
Reusable code in 06 functions
Posted