switch and match

A if/elseif chain grows one branch at a time. When one value in PHP selects between many outcomes, switch and match say the same thing in a shape that reads like a table.

The switch statement

The switch statement compares one value against many case labels. When a label matches, execution starts there and continues downward until a break stops it. It fits decisions where one variable selects between many outcomes.

<?php $role = "editor"; switch ($role) { case "admin": echo "full access", PHP_EOL; break; case "editor": echo "can edit", PHP_EOL; break; default: echo "read only", PHP_EOL; }

php switch.php

can edit

Leaving out break is called fall-through: PHP keeps running into the next case. Use it only on purpose, with a comment that says so, because silent fall-through is a classic source of bugs. Two case labels above one block share it on purpose:

<?php switch ("editor") { case "admin": case "editor": // both keep the staff area echo "staff area", PHP_EOL; break; default: echo "guest area", PHP_EOL; }

php fallthrough.php

staff area

The role "admin" would run the same line, because its empty label falls into the block below it without printing anything of its own.

The match expression

The match expression does the same job but returns a value, so you assign the result instead of echoing inside every arm. Arms use =>, accept comma-separated conditions, and never fall through.

Because it is an expression, match fits anywhere a value fits: on the right side of an assignment, inside a function call, or inside another expression.

<?php $role = "editor"; $label = match ($role) { "admin" => "full access", "editor", "author" => "can edit", default => "read only", }; echo $label, PHP_EOL;

php match.php

can edit

The second arm lists two labels and one value, so the editor and the author reach the same result and no break is needed anywhere.

Strict comparison

The two constructs compare differently: switch uses loose == comparison while match uses strict === comparison of value plus type. The string "200" therefore matches the integer 200 in a switch, but not in a match.

<?php $code = "200"; var_dump($code == 200); switch ($code) { case 200: echo "switch: integer branch", PHP_EOL; break; } $result = match ($code) { 200 => "match: integer arm", "200" => "match: string arm", default => "match: other", }; echo $result, PHP_EOL;

php strict.php

bool(true)
switch: integer branch
match: string arm

The loose check turns the string into a number and says yes, so the switch takes the 200 case. The match keeps the type and skips that arm for the one that spells the string.

Default arms

A switch without a matching case and without default silently does nothing. A match without a matching arm and without default throws UnhandledMatchError instead. Always add a default arm unless the exception is exactly what you want.

<?php $action = "preview"; $message = match ($action) { "save" => "saved", "delete" => "deleted", default => "unknown action", }; echo $message, PHP_EOL;

php default.php

unknown action

The default arm catches the action the two labels do not name. Without it, this run ended with an Unhandled match case error instead of printing the line.

Exercises

  1. Change $role in switch.php to "guest" and run it. Which branch runs, and why?
  2. Remove the break after "can edit" and run switch.php with "editor". What does the extra line show?
  3. Rewrite the switch of switch.php as a match expression in one assignment, then print the result.
  4. Delete the default arm of default.php and run it. Which error do you get, and what is its class?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a switch need after the statements of a case, unless the fall-through is intended?

    Answer

    break

    Without a break the execution falls through into the next case, which is intended only when the labels share a block.

  2. What does a match expression give that a switch statement does not?

    Answer

    A value, so it can sit on the right of an assignment

    match is an expression: it returns the value of the chosen arm, and every arm holds exactly one expression.

  3. Which comparison does match use?

    Answer

    Strict ===

    match compares with ===, so a string arm and an integer arm never answer for each other.

  4. What happens when no arm of a match without default matches?

    Answer

    It throws UnhandledMatchError

    A match must produce a value, so an unmatched case raises UnhandledMatchError; only a default arm can catch it.

  5. What does this program print? <?php $role = "admin"; switch ($role) { case "admin": echo "full access"; break; case "editor": echo "can edit"; break; default: echo "read only"; }

    Answer

    full access

    The first label matches and its break stops the switch before the other cases are reached.

  6. What does this program print? <?php $role = "editor"; $label = match ($role) { "admin" => "full access", "editor", "author" => "can edit", default => "read only", }; echo $label;

    Answer

    can edit

    Comma-separated labels share one arm, and the editor reaches it, so the expression is "can edit".

  7. What does this program print? <?php switch ("1") { case 1: echo "one"; break; }

    Answer

    one

    switch compares loosely, so the string "1" matches the integer case 1 and prints one.

  8. What does this program print? <?php $label = match ("1") { 1 => "int", "1" => "str", }; echo $label;

    Answer

    str

    match compares strictly, so the integer arm is skipped and the string arm answers.

  9. What does this program print? <?php $size = match (3) { 1, 2 => "low", 3, 4 => "high", default => "other", }; echo $size;

    Answer

    high

    The arm with the labels 3 and 4 holds the value 3, so the match returns high.

  10. What does this program print? <?php switch ("guest") { case "admin": echo "admin"; break; } echo "done";

    Answer

    done

    A switch with no matching case and no default does nothing at all, so only the line after it prints.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev