Field Manual PHP / Laravel reference
← PHP

Match expressions

match returns a value using strict comparisons.

What it is

match returns a value using strict comparisons.

When to use it

Map finite values without switch fall-through.

Syntax

$label = match ($status) {
    "paid" => "Settled",
    "pending" => "Awaiting payment",
    default => "Unknown",
};

Example

$httpStatus = match ($outcome) {
    "created" => 201,
    "duplicate" => 200,
    "conflict" => 409,
    default => 500,
};

Common mistakes

Unhandled values throw UnhandledMatchError without default; 1 and "1" differ.

Related topics

Browse PHP