Core Concepts
Control Flow
Topic 4 of 13
PHP
<?php
// if / elseif / else
$score = 85;
if ($score >= 90) {
echo "A grade";
} elseif ($score >= 80) {
echo "B grade";
} else {
echo "Below B";
}
// match (PHP 8) โ strict, no fall-through, returns value
$result = match(true) {
$score >= 90 => 'A',
$score >= 80 => 'B',
$score >= 70 => 'C',
default => 'F',
};
// Nullsafe operator (PHP 8) โ safe chaining
$city = $user?->getAddress()?->city;
// Alternative syntax for templates
if ($loggedIn):
?>
<p>Welcome back!</p>
<?php endif;
๐ก match vs switch
match uses strict (===) comparison, returns a value, no break needed, and throws UnhandledMatchError if no arm matches. Always prefer match in PHP 8+.