12. PHP Switch and Match

What you will learn

  • How switch works, and the need for break
  • The modern match expression
  • How they differ
  • When to use each

The switch statement

switch compares one value against several cases. Each case needs a break, or execution falls through into the next case. default runs when nothing matches.

PHP code

<?php
$day = 3;

switch ($day) {
    case 1:
        $name = "Monday";
        break;
    case 2:
        $name = "Tuesday";
        break;
    case 3:
        $name = "Wednesday";
        break;
    default:
        $name = "Another day";
}

echo "Day $day is $name";

Output

Day 3 is Wednesday

Grouping cases

Leaving out break on purpose lets several cases share code.

PHP code

<?php
$day = "Sat";

switch ($day) {
    case "Sat":
    case "Sun":
        echo "Weekend";
        break;
    default:
        echo "Weekday";
}

Output

Weekend

A catch with switch

switch compares with loose equality (==), so type conversion can cause surprises. It also needs many lines for simple jobs.

The match expression (PHP 8)

match is a shorter, safer alternative. It returns a value, uses strict comparison (===), needs no break, and throws an error if no arm matches and there is no default.

PHP code

<?php
$day = 3;

$name = match ($day) {
    1 => "Monday",
    2 => "Tuesday",
    3 => "Wednesday",
    default => "Another day",
};

echo "Day $day is $name";

Output

Day 3 is Wednesday

The comma-separated arms mean the same as the long switch, in a quarter of the lines. Remember the semicolon after the closing brace.

Several values in one arm

PHP code

<?php
$day = "Sun";

echo match ($day) {
    "Sat", "Sun" => "Weekend",
    "Mon", "Tue", "Wed", "Thu", "Fri" => "Weekday",
    default => "Unknown",
};

Output

Weekend

match(true) for ranges

To test conditions rather than exact values, match against true.

PHP code

<?php
$score = 72;

$grade = match (true) {
    $score >= 90 => "A",
    $score >= 70 => "B",
    $score >= 50 => "C",
    default => "Try again",
};

echo $grade;

Output

B

What if nothing matches?

Without a default, an unmatched match throws an UnhandledMatchError. That is useful: it stops silent mistakes. Add a default when an unknown value is expected.

switch versus match

switch match
Comparison Loose (==) Strict (===)
Returns a value No Yes
Needs break Yes No
No match, no default Does nothing Throws an error
Can run several statements Yes One expression per arm
PHP version All 8.0 and later

match needs PHP 8. If your host runs an older version, use switch or if. Always run a supported PHP version.

Common mistakes

  • Forgetting break in a switch.
  • Forgetting the semicolon after a match block.
  • Expecting match to convert types. "1" does not match 1.
  • Leaving out default when unexpected values are possible.
  • Putting several statements in a match arm. Use switch or if for that.

Exercise

Use match to turn $code into a message: 200 gives OK, 404 gives Not found, 500 gives Server error, anything else gives Unknown. Test with 404.

Show solution
<?php
$code = 404;

echo match ($code) {
    200 => "OK",
    404 => "Not found",
    500 => "Server error",
    default => "Unknown",
};

The output is Not found.

Knowledge check

1. What happens if you forget break in a switch?

Execution falls through into the next case.

2. Which comparison does match use?

Strict comparison (===).

3. What does match do when nothing matches and there is no default?

It throws an UnhandledMatchError.

Summary

  • switch compares loosely and needs break.
  • match (PHP 8) is shorter, strict, and returns a value.
  • Use match(true) for ranges, and include default when needed.
Scroll to Top