Match Expressions
Introduced in PHP 8.0, match is an expression (not a statement) that compares a subject value against a list of conditions and returns a value, similar in spirit to switch but fixing several of its long-standing footguns. Unlike switch, match uses strict comparison (===) by default, has no fallthrough between arms, and requires every possible case to be handled — if no arm matches and there is no default, PHP throws an UnhandledMatchError at runtime rather than silently doing nothing.
Cricket analogy: PHP's match is like DRS using strict Hawk-Eye precision (===) rather than the naked-eye umpire's call (switch's loose ==) — there's no 'benefit of the doubt' fallthrough to the next appeal, every possible outcome (out, not out, umpire's call) must be covered, or the review system itself throws an error rather than staying silent.
match vs switch
Because match is an expression, its result can be assigned directly to a variable, returned from a function, or passed as an argument — you never need the awkward 'declare a variable above, assign inside each case, break' pattern that switch requires. Multiple values can share one arm by separating them with commas, and match(true) is a popular idiom for expressing arbitrary boolean conditions (range checks, type checks) rather than exact-value matches, replacing long elseif chains.
Cricket analogy: Because match is an expression, a scorer can directly assign 'result = match(runsNeeded) {...}' instead of the old declare-then-assign-in-each-case dance; grouping 4 and 6 on one arm as 'boundary' is like crediting both a four and a six as 'boundary hit', and match(true) lets you write range checks like 'runsNeeded <= 6' directly, replacing a long elseif chain of run-rate conditions.
<?php
declare(strict_types=1);
enum OrderStatus {
case Pending;
case Shipped;
case Delivered;
case Cancelled;
}
function statusLabel(OrderStatus $status): string {
return match ($status) {
OrderStatus::Pending => 'Awaiting confirmation',
OrderStatus::Shipped => 'On its way',
OrderStatus::Delivered => 'Complete',
OrderStatus::Cancelled => 'Cancelled',
};
}
function sizeLabel(int $itemCount): string {
return match (true) {
$itemCount === 0 => 'Empty',
$itemCount < 5 => 'Small order',
$itemCount < 20 => 'Medium order',
default => 'Bulk order',
};
}
echo statusLabel(OrderStatus::Shipped) . PHP_EOL;
echo sizeLabel(12) . PHP_EOL;
Combining match with enums (both PHP 8.1+) is a very common and powerful pairing: since enum cases are a closed, known set of values, the compiler-like exhaustiveness of match (throwing UnhandledMatchError on gaps) helps you catch missing cases immediately during development or testing rather than in production.
match uses strict comparison, so match("1") { 1 => 'int one', "1" => 'string one' } will match the "1" arm, not the 1 arm — unlike switch, which would loosely match either. Don't port switch logic to match without checking whether any case relied on loose-equality coercion.
- match is an expression that returns a value; switch is a statement that requires manual variable assignment.
- match compares using strict equality (===), unlike switch's loose (==) comparison.
- There is no fallthrough in match — each arm is self-contained and no break is needed.
- An unmatched subject with no default arm throws an UnhandledMatchError at runtime.
- Comma-separated conditions let multiple values share a single match arm.
- match(true) with boolean conditions is a clean replacement for long if/elseif chains.
Practice what you learned
1. What comparison operator does match use to compare the subject against each arm's condition?
2. What happens if no arm in a match expression matches the subject and there is no default?
3. Is match a statement or an expression?
4. Does match require a break keyword between arms?
5. What is the match(true) idiom typically used for?
Was this page helpful?
You May Also Like
Conditionals in PHP
Covers if/elseif/else, the ternary and null-coalescing shortcuts, switch statements, and alternative control-structure syntax used in templates.
Enums in PHP
PHP 8.1 enums provide a first-class way to define a fixed set of named values, with optional scalar backing, methods, interfaces, and constants attached.
Operators and Expressions
Explains PHP's arithmetic, comparison, logical, assignment, and null-coalescing operators, along with operator precedence and expression evaluation order.
Loops in PHP
Explains PHP's for, while, do-while, and foreach loops, along with break, continue, and reference-based iteration pitfalls.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics