Master programming syntax, solve interactive challenges & run live code in Syntaxis Explore Arena →
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

NameTypeDescription
subject_expressionmixedThe 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 Found
Unlike switch, match is an expression that returns a value, uses strict === comparison, does not fall through, and throws UnhandledMatchError if no arm matches.