← All posts

PHP match vs switch: which to use, and the loose comparison trap that catches people out

PHP match vs switch explained with tested examples: strict comparison, no fall-through, UnhandledMatchError, and match(true) for ranges.

Ian Croasdell of PHP AIR working at a laptop with his tabby cat Simba sitting beside him on the desk

If you learned PHP before version 8, you probably reach for switch without thinking. Since PHP 8.0 there's match, and for most "turn this value into that value" jobs it's the better tool. Here's the practical difference, with examples that run on current PHP 8.

The one difference that matters most: strict comparison

switch compares loosely (==). match compares strictly (===). That sounds academic until a form value turns up as a string.

```php <?php $status = '1'; switch ($status) { case 1: echo "switch: active\n"; break; default: echo "switch: unknown\n"; } echo match ($status) { 1 => "match: active\n", default => "match: unknown\n", }; ```

Output:

``` switch: active match: unknown ```

The switch treats the string '1' as equal to the integer 1. The match doesn't, because the types differ. Neither is "wrong", but match makes you decide what type you're dealing with, which is usually what you wanted all along. If your input comes from a request, cast or validate it first, then match on the clean value.

match returns a value

switch is a statement: you assign inside each case and remember the break. match is an expression, so it returns a value you can assign, return or echo directly. There's no fall-through and no break to forget. You can list several values on one arm with commas.

```php <?php function deliveryLabel(string $code): string { return match ($code) { 'std' => 'Standard (3-5 days)', 'exp', 'next' => 'Express', 'col' => 'Click and collect', }; } echo deliveryLabel('next'), "\n"; try { echo deliveryLabel('drone'), "\n"; } catch (\UnhandledMatchError $e) { echo get_class($e), ': ', $e->getMessage(), "\n"; } ```

Output:

``` Express UnhandledMatchError: Unhandled match case 'drone' ```

No default? You get an error, not silence

That second call is the bit I like most. A switch with no matching case and no default just does nothing, and your variable quietly stays unset or keeps an old value. A match with no matching arm throws UnhandledMatchError. That's a feature: a new delivery code nobody told you about fails loudly in testing instead of printing a blank label on an invoice.

Add a default arm only when there genuinely is a sensible fallback. Don't add one just to make the error go away.

Ranges with match(true)

match checks each arm in order and takes the first one that is identical to the subject. Pass true as the subject and each arm can be a condition:

```php <?php function band(int $score): string { return match (true) { $score >= 70 => 'First', $score >= 60 => 'Upper second', $score >= 50 => 'Lower second', $score >= 40 => 'Third', default => 'Fail', }; } foreach ([82, 65, 40, 12] as $s) { echo $s, ': ', band($s), "\n"; } ```

Output:

``` 82: First 65: Upper second 40: Third 12: Fail ```

Order matters here: put the highest threshold first. Each condition must produce a real boolean, because match compares with ===, so something like $score on its own (an int) would never equal true.

So when would I still use switch?

  • When each case needs several statements, not just a value. A match arm is a single expression, so you'd end up calling a function from each arm anyway.
  • When you deliberately want fall-through between cases (rare, and worth a comment if you do).
  • When you're working in older code and a consistent style matters more than a small win.

For mapping one value to another, though, match is shorter, stricter and harder to get wrong.

Want more like this?

This kind of "do it the safe way by default" thinking runs right through my book, PHP Done The Correct Way, out in paperback on Amazon UK and on Kindle, with a free companion code page. If you'd rather have someone tidy up an older PHP site for you, PHP AIR builds and maintains websites and booking systems for real businesses.

Let's talk More posts