Why This Happens
PHP 8.0's match expression is strict and exhaustive. Unlike switch, if no arm matches the given value and there is no default arm, PHP throws UnhandledMatchError. This is actually a safety feature that catches missing cases at runtime.
The Problem
function statusLabel(string $status): string {
return match ($status) {
'active' => 'Active',
'inactive' => 'Inactive',
};
}
echo statusLabel('pending'); // No matching armThe Fix
function statusLabel(string $status): string {
return match ($status) {
'active' => 'Active',
'inactive' => 'Inactive',
'pending' => 'Pending',
default => 'Unknown',
};
}Step-by-Step Fix
- 1
Identify the unmatched value
Check what value was passed to the match expression. Add logging or inspect the variable before the match.
- 2
Add the missing case
If the value is a legitimate case, add a new arm to the match expression to handle it.
- 3
Add a default arm
Add a default arm to handle unexpected values gracefully, either returning a fallback or throwing a descriptive exception.
Bugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free