100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
PHP

Match Expressions

Introduces PHP 8's match expression, its strict comparison semantics, and why it is often a safer, more concise replacement for switch.

Control FlowIntermediate7 min readJul 9, 2026
Analogies

Match Expressions

Introduced in PHP 8.0, match is an expression (not a statement) that compares a subject value against a list of conditions and returns a value, similar in spirit to switch but fixing several of its long-standing footguns. Unlike switch, match uses strict comparison (===) by default, has no fallthrough between arms, and requires every possible case to be handled — if no arm matches and there is no default, PHP throws an UnhandledMatchError at runtime rather than silently doing nothing.

🏏

Cricket analogy: PHP's match is like DRS using strict Hawk-Eye precision (===) rather than the naked-eye umpire's call (switch's loose ==) — there's no 'benefit of the doubt' fallthrough to the next appeal, every possible outcome (out, not out, umpire's call) must be covered, or the review system itself throws an error rather than staying silent.

match vs switch

Because match is an expression, its result can be assigned directly to a variable, returned from a function, or passed as an argument — you never need the awkward 'declare a variable above, assign inside each case, break' pattern that switch requires. Multiple values can share one arm by separating them with commas, and match(true) is a popular idiom for expressing arbitrary boolean conditions (range checks, type checks) rather than exact-value matches, replacing long elseif chains.

🏏

Cricket analogy: Because match is an expression, a scorer can directly assign 'result = match(runsNeeded) {...}' instead of the old declare-then-assign-in-each-case dance; grouping 4 and 6 on one arm as 'boundary' is like crediting both a four and a six as 'boundary hit', and match(true) lets you write range checks like 'runsNeeded <= 6' directly, replacing a long elseif chain of run-rate conditions.

php
<?php
declare(strict_types=1);

enum OrderStatus {
    case Pending;
    case Shipped;
    case Delivered;
    case Cancelled;
}

function statusLabel(OrderStatus $status): string {
    return match ($status) {
        OrderStatus::Pending   => 'Awaiting confirmation',
        OrderStatus::Shipped   => 'On its way',
        OrderStatus::Delivered => 'Complete',
        OrderStatus::Cancelled => 'Cancelled',
    };
}

function sizeLabel(int $itemCount): string {
    return match (true) {
        $itemCount === 0 => 'Empty',
        $itemCount < 5   => 'Small order',
        $itemCount < 20  => 'Medium order',
        default          => 'Bulk order',
    };
}

echo statusLabel(OrderStatus::Shipped) . PHP_EOL;
echo sizeLabel(12) . PHP_EOL;

Combining match with enums (both PHP 8.1+) is a very common and powerful pairing: since enum cases are a closed, known set of values, the compiler-like exhaustiveness of match (throwing UnhandledMatchError on gaps) helps you catch missing cases immediately during development or testing rather than in production.

match uses strict comparison, so match("1") { 1 => 'int one', "1" => 'string one' } will match the "1" arm, not the 1 arm — unlike switch, which would loosely match either. Don't port switch logic to match without checking whether any case relied on loose-equality coercion.

  • match is an expression that returns a value; switch is a statement that requires manual variable assignment.
  • match compares using strict equality (===), unlike switch's loose (==) comparison.
  • There is no fallthrough in match — each arm is self-contained and no break is needed.
  • An unmatched subject with no default arm throws an UnhandledMatchError at runtime.
  • Comma-separated conditions let multiple values share a single match arm.
  • match(true) with boolean conditions is a clean replacement for long if/elseif chains.

Practice what you learned

Was this page helpful?

Topics covered

#PHP#PHPProgrammingStudyNotes#Programming#MatchExpressions#Match#Expressions#Switch#StudyNotes#SkillVeris#ExamPrep