Reference / PHP
match expression
A control structure introduced in PHP 8.0 that evaluates an expression and returns a value based on strict comparison (===) pattern matching.
Syntax
match (subject_expression) { pattern => expression, default => default_expression }Parameters
| Name | Type | Description |
|---|---|---|
| subject_expression | mixed | The value to match against. |
Examples
HTTP status mapper
$statusCode = 404;
$message = match ($statusCode) {
200, 201 => 'Success',
400 => 'Bad Request',
404 => 'Not Found',
default => 'Internal Server Error',
};
echo $message; // Not FoundUnlike switch, match is an expression that returns a value, uses strict === comparison, does not fall through, and throws UnhandledMatchError if no arm matches.