← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 15 of 24

PHP Match Expression Advanced

Advanced match uses the strict lookup chart in more clever ways, like matching on conditions instead of exact values. It gives a clean answer without the risk of falling through to the next choice.
Syntax
php
$result = match(true) {
    condition1 => result1,
    condition2, condition3 => result2,
    default => default_result,
};

What is the match Expression?

match is a stricter, more concise alternative to switch: it's an expression that returns a value directly, requires no break statements, and can't accidentally fall through to the next case.

Example: What is the match Expression?

php
<?php
// Declare `$grade`, set to `85`
$grade = 85;
$letter = match(true) {
    $grade >= 90 => 'A',
    $grade >= 80 => 'B',
    default => 'C',
};
// Print `$letter` to the output
echo $letter;
?>

Multiple Matches

You can list several comma-separated values on one match arm to share a single result, which avoids the repetition switch would otherwise require for each fall-through case.

Example: Multiple Matches

php
<?php
// Declare `$day`, set to `6`
$day = 6;
$type = match($day) {
    1, 2, 3, 4, 5 => 'Weekday',
    6, 7 => 'Weekend',
};
// Print `$type` to the output
echo $type;
?>

Strict Type Matching

Unlike switch's loose == comparison, match compares with strict === semantics, checking type as well as value -- so match(true) and matching 1 against 1 behave very differently than in a switch statement.

Example: Strict Type Matching

php
<?php
// Declare `$value`, set to '1'
$value = '1';
$result = match(true) {
    $value === 1 => 'integer one',
    $value === '1' => 'string one',
};
// Print `$result` to the output
echo $result;
?>

The default Match Fallback

Omitting a default arm means match throws an UnhandledMatchError the moment no arm's value matches the subject, which surfaces unexpected inputs immediately instead of silently doing nothing like an unmatched switch would.

Example: The default Match Fallback

php
<?php
// Declare `$code`, set to `99`
$code = 99;
// Try running this block; jump to `catch` if it throws
try {
    // Print `match($code) {` to the output
    echo match($code) {
        1 => 'One',
    };
// Catch \UnhandledMatchError $e
} catch (\UnhandledMatchError $e) {
    // Print `"Unhandled: " . $e->getMessage()` to the output
    echo "Unhandled: " . $e->getMessage();
}
?>

Throwing Exceptions inside match

Because match is an expression, you can put a throw statement directly inside an arm to reject an invalid case right where it's detected, keeping validation logic compact and colocated with the other arms.

Example: Throwing Exceptions inside match

php
<?php
// Define the function `checkStatus` taking `$status`
function checkStatus($status) {
    // Return `match($status) {` from this function
    return match($status) {
        'active', 'pending' => true,
        default => throw new InvalidArgumentException("Invalid status: $status"),
    };
}
// Try running this block; jump to `catch` if it throws
try {
    // Call `checkStatus('deleted')`
    checkStatus('deleted');
// Catch InvalidArgumentException $e
} catch (InvalidArgumentException $e) {
    // Print `$e->getMessage()` to the output
    echo $e->getMessage();
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting match to fall through like switch, when each arm returns and stops.
  2. Not adding a default arm and getting an UnhandledMatchError for values that match nothing.
  3. Forgetting that match uses strict comparison, so "1" does not match 1.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.