match Expression (PHP 8.0)
Also Known As
PHP match
match expression PHP 8
match vs switch
TL;DR
A stricter, expression-based alternative to switch that uses strict comparison, returns a value, and throws on unmatched input.
Explanation
The match expression was introduced in PHP 8.0 as a replacement for switch with three key improvements: it uses strict (===) comparison instead of loose (==), each arm is an expression returning a value (no break needed), and it throws an UnhandledMatchError if no arm matches (unlike switch which silently falls through). Multiple conditions can be comma-separated in a single arm. match is particularly useful for mapping one value to another without mutation.
Common Misconception
✗ match is just switch with cleaner syntax. match uses strict comparison (===) while switch uses loose comparison (==) — this prevents type juggling bugs. match also throws UnhandledMatchError for unmatched values instead of silently falling through, making it safer by default.
Why It Matters
Match expressions use strict comparison, return a value, and throw an UnhandledMatchError for unmatched cases — all three of which switch statements silently get wrong. They eliminate entire classes of bugs that switch creates through type coercion and fall-through.
Common Mistakes
- Using match when you need fall-through across multiple conditions — match has no fall-through by design.
- Forgetting match throws UnhandledMatchError for unmatched values — always include a default arm if exhaustiveness is not guaranteed.
- Using match for complex multi-statement logic per arm — it returns a single expression, use if/else for multi-line branches.
- Assuming match works like switch for loose comparisons — match is always strict (===).
Code Examples
💡 Note
match() returns a value (assign or return directly), never falls through, and uses strict comparison — three improvements over switch.
✗ Vulnerable
// switch with type coercion — '1' matches 1:
switch ($status) {
case 1: return 'active'; // '1' also matches — loose comparison
case 0: return 'inactive';
}
// match uses strict comparison:
return match($status) {
1 => 'active',
0 => 'inactive',
default => throw new UnhandledMatchError(),
};
✓ Fixed
// match is strict (===), exhaustive, and expression-based
$label = match($order->status) {
'pending' => 'Awaiting payment',
'paid' => 'Processing',
'shipped' => 'On the way',
'cancelled' => 'Cancelled',
default => throw new \UnexpectedValueException("Unknown status: {$order->status}"),
};
// No default = UnhandledMatchError on unmatched value — catches missing cases at runtime
// PHPStan/Psalm can also verify exhaustiveness at analysis time
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
28
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
No pings yesterday
Amazonbot 7
Perplexity 7
Unknown AI 3
Ahrefs 2
Majestic 1
Google 1
SEMrush 1
Also referenced
How they use it
crawler 21
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Replace switch statements with match() when returning a value — match uses strict comparison and is exhaustive (throws UnhandledMatchError)
📦 Applies To
PHP 8.0+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
switch statement returning a value from each case — candidate for match expression
Auto-detectable:
✓ Yes
rector
phpcs
php-cs-fixer
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: Medium
✓ Auto-fixable
Fix: Low
Context: Function