📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PHP for Beginners Match Expression

Match Expression

5 min read Quiz at the end
The match expression is a cleaner alternative to switch, introduced in PHP 8. It uses strict comparison, returns a value directly, and throws an error if no arm matches. It prevents accidental fall-through bugs.

Match Expression (PHP 8)

// Old way — switch
switch ($status) {
    case "active":   $label = "Active"; break;
    case "inactive": $label = "Inactive"; break;
    default:         $label = "Unknown";
}

// New way — match (strict comparison, no fallthrough)
$label = match($status) {
    "active"   => "Active",
    "inactive" => "Inactive",
    "banned"   => "Banned",
    default    => "Unknown"
};

// Multiple conditions per arm
$discount = match(true) {
    $total >= 500 => 0.20,
    $total >= 200 => 0.10,
    $total >= 100 => 0.05,
    default       => 0.00,
};

// Throws UnhandledMatchError if no match and no default
Topic Quiz · 5 questions

Test your understanding before moving on

1. What is the key difference between match and switch?
💡 match uses strict type comparison and each arm must match exactly one value.
2. Does match have fallthrough like switch?
💡 Unlike switch, match has no fallthrough — each arm is self-contained.
3. What error does an unhandled match throw?
💡 match throws UnhandledMatchError if no arm matches and no default is provided.
4. Can match return a value?
💡 match is an expression — it returns the value of the matched arm.
5. Which is correct match syntax?
💡 match($x) { value => result, default => fallback }