Originally published on zero2php.dev — part of the Zero to PHP course. View this lesson on the original site.
match vs switch
PHP 8+ match is an expression (it returns a value) and comparisons use strict (===) semantics.
Example
<?php
declare(strict_types=1);
$x = 2;
echo match ($x) {
1 => 'one',
2 => 'two',
default => 'many',
}, "\n";
When to use which
Use match when a single value maps to a result; use switch when imperative steps differ wildly (still valid).
Exercise
Set $n = 3. Use match to echo gamma when $n is 3 (other arms can return placeholders).
From Zero to PHP — Control flow, functions, arrays, and the web