Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

You've probably spent a lot of time using switch statements. They get the job done, but if you've ever forgotten a break and spent twenty minutes wondering why your code is executing five different blocks of logic, you'll appreciate why match was added to PHP 8.0. Think of it as a more modern, stricter, and more concise cousin of the switch.

How is this actually different from a switch statement?

The biggest difference is that match is an expression, not a statement. In plain English: match returns a value. You can assign the result directly to a variable or return it from a function without having to manually update a variable inside every single case.

Another critical detail is that match uses strict comparison (===), whereas switch uses loose comparison (==). This saves you from those weird PHP bugs where the integer 0 somehow matches a string "0" or a null value.

// The old switch way
switch ($paymentStatus) {
    case 'paid':
        $message = 'Thank you for your purchase!';
        break;
    case 'pending':
        $message = 'Your payment is processing.';
        break;
    default:
        $message = 'Payment failed.';
        break;
}

// The match way
$message = match ($paymentStatus) {
    'paid'    => 'Thank you for your purchase!',
    'pending' => 'Your payment is processing.',
    default   => 'Payment failed.',
};

Do I still need to use break?

Nope. Forget about break entirely. One of the most annoying things about switch is "fall-through," where the code keeps running into the next case if you forget that one word. match doesn't do that. Once it finds a match, it returns the value and stops immediately. It's much cleaner and significantly less prone to human error.

What happens if none of the arms match the value?

This is where you have to be careful. If match doesn't find a matching arm and you haven't provided a default, PHP will throw an UnhandledMatchError. This might seem annoying, but as an engineer, I actually love this. It forces you to be explicit about every possible state your data can be in, which means fewer "silent failures" in production.

I always recommend adding a default arm unless you are 100% certain the input is restricted to a specific set of values (like an Enum).

Can I handle multiple values for one result?

Yes, and it's way more elegant than stacking case statements on top of each other. You just separate the values with commas. It's essentially an "OR" operation.

$userRole = 'editor';

$accessLevel = match ($userRole) {
    'admin', 'superadmin' => 10,
    'editor', 'author'    => 5,
    'subscriber'          => 1,
    default               => 0,
};

echo "Access level: $accessLevel"; // Outputs: Access level: 5



📋 Practical Task

Build an HTTP Response Status Mapper

You are building a debugging tool that takes a numeric HTTP response code and converts it into a human-readable category. Create a script that uses a match expression to map the following codes:

  • 200 and 201 should return "Success"
  • 400, 401, and 403 should return "Client Error"
  • 404 should return "Not Found"
  • 500 and 503 should return "Server Error"
  • Any other code should return "Unknown Status"

Test your solution by assigning a code to a variable (e.g., $code = 404;) and printing the result of the match expression.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.