switch and match

switch and the match Expression

switch jumps to the first case loosely equal (==) to its subject and runs everything below, through later cases, until a break or return; a forgotten break is the classic bug. match (PHP 8.0) compares with ===, never falls through, and is an expression you can return or assign. An arm may list several values, and match (true) replaces an if/elseif ladder.

The same lookup as switch and as matchPHP
<?php
function viaSwitch(mixed $s): string
{
    switch ($s) {                    // compares with ==
        case 'new':
        case 'pending':              // two cases share one body
            return 'awaiting payment';
        case 1:
            return 'legacy code 1';
    }
    return 'unknown';
}
$viaMatch = fn(mixed $s): string => match ($s) {  // compares with ===
    'new', 'pending' => 'awaiting payment', 1 => 'legacy code 1',
};
$band = fn(int $n): string => match (true) {      // the first true arm wins
    $n >= 90 => 'A', $n >= 70 => 'C', default => 'F',
};
echo viaSwitch('1'), " | ", $viaMatch(1), " | ", $band(84), "\n";
echo $viaMatch('1'), "\n";            // "1" !== 1 and there is no default arm
Output
legacy code 1 | legacy code 1 | C
PHP Fatal error:  Uncaught UnhandledMatchError: Unhandled match case of type string in
/tmp/ch04-03/switch.php:13
...

switch let the string '1' into case 1; match refused it and, with no default, threw UnhandledMatchError instead of silently doing nothing. The message hides the value because Ubuntu 225 's php.ini sets zend.exception_ignore_args = On; php -n prints Unhandled match case '1'.