PHP has the standard control-flow constructs — conditionals (, , ), loops (, , ), and branching (, ) — plus a couple of distinctive features like for arrays and the modern expression.
ifswitchmatchforforeachwhilebreakcontinueforeachmatch<?php
if ($score >= 90) {
$grade = "A";
} elseif ($score >= 80) {
$grade = "B";
} else {
$grade = "F";
}
// ternary and null coalescing
$status = $age >= 18 ? "adult" : "minor";
$name = $input ?? "default"; // ?? — use default if null/unset (PHP 7+)
$value ??= "x"; // ??= assign if null (PHP 7.4)
The null coalescing operator ?? is heavily used in PHP for defaults (returns the right side if the left is null or unset) — cleaner than isset() checks.
switch ($day) {
case "Sat":
case "Sun":
$type = "weekend"; break; // ⚠️ break needed or it falls through
default:
$type = "weekday";
}
// match (PHP 8) — cleaner: returns a value, STRICT comparison, NO fall-through
$type = match($day) {
"Sat", "Sun" => "weekend", // multiple values, no break needed
default => "weekday",
};
The match expression (PHP 8) is a modern improvement over switch: it returns a value, uses strict (===) comparison, and has no fall-through (no break needed) — preferred in new code.
for ($i = 0; $i < 10; $i++) { }
foreach ($items as $item) { } // iterate array values
foreach ($map as $key => $value) { } // keys + values — the PHP idiom for arrays
while ($condition) { }
do { } while ($condition);
foreach is the idiomatic, most-used loop in PHP for iterating arrays (with the $key => $value form for associative arrays).
foreach ($items as $item) {
if ($item->skip) continue; // skip to next iteration
if ($item->stop) break; // exit the loop
}
Control flow is the basic mechanism for expressing logic, used in every program, so understanding PHP's constructs is fundamental everyday knowledge.
Most are standard, but a few PHP-specific points matter: foreach is the idiomatic, dominant loop for iterating PHP's ubiquitous arrays (with the $key => $value form for maps); the null coalescing operator ?? (and ??=) is heavily used for clean default handling (a distinctly PHP idiom); and the modern match expression (PHP 8) is an important improvement over the error-prone switch — returning a value, using strict comparison, and eliminating fall-through bugs (the classic switch pitfall of forgetting break).
Knowing these constructs, and preferring the modern, safer forms (match over switch, ?? for defaults), is everyday knowledge for writing clean, idiomatic, bug-free PHP logic, reflecting fluency with both the fundamentals and modern PHP's improvements.