Lesson

PHP Switch

10 min reading
Free Course

PHP Switch and Match: Clean Multi-Branch Selection

The PHP switch statement compares one expression against several case values and runs the block that matches. Since PHP 8, the match expression offers a stricter and shorter alternative when each branch simply produces a value. This guide shows how both work, where switch can surprise you, and how to choose between them.

What Is the PHP Switch Statement?

A switch evaluates its expression once, then checks the case labels from top to bottom. When one matches, PHP starts running code from that point. It keeps going until it reaches a break or the closing brace. If no case matches, the default block runs, if there is one.

Think of it as a tidier replacement for a long if / elseif / else chain that tests the same variable against different values.

Standard switch Statement with break

Include break; at the end of every case block unless you intentionally want execution to fall through to the next case. Always add a default: branch to handle unexpected inputs safely.

<?php
declare(strict_types=1);

$httpMethod = "POST";

switch ($httpMethod) {
    case "GET":
        echo "Fetching resource records..." . PHP_EOL;
        break;
    case "POST":
        echo "Creating a new resource..." . PHP_EOL;
        break;
    case "PUT":
    case "PATCH":
        echo "Updating an existing resource..." . PHP_EOL;
        break;
    default:
        echo "Method not supported." . PHP_EOL;
        break;
}

Grouping case "PUT": and case "PATCH": together without a break between them lets two inputs share one handler cleanly.

Modern PHP 8 match Expression

Unlike switch, which uses loose equality (==) and requires break, match uses strict comparison (===), returns a value directly, and throws an UnhandledMatchError if no arm matches.

<?php
declare(strict_types=1);

$statusCode = 404;

$message = match ($statusCode) {
    200, 201 => "Request succeeded",
    400 => "Bad request payload",
    401, 403 => "Authentication or permission error",
    404 => "Resource not found",
    default => "Unexpected server status",
};

echo $message . PHP_EOL;

How to Check That Your Code Works

  1. Run php -v to confirm your PHP version. match needs 8.0 or newer, and older versions give a parse error.

  2. Test every branch, including default, with at least one input each.

  3. Try awkward inputs: a number as a string ("404" vs 404), null, an empty string, and an unexpected value.

  4. Formatch, remove the default arm temporarily and confirm an UnhandledMatchError is thrown for an unknown input.

  5. Use var_dump() on the result to see the type, not just the printed text.

Common Mistakes to Avoid

  • Forgetting break in switch: Without break, PHP continues executing every following case block until it hits a break or the closing brace.

  • Relying on switch loose equality with 0 or "": switch (0) loosely matches case "guest": in older PHP versions or unexpected numeric strings. Use match for strict type checks.

  • Using continue inside a switch inside a loop: In PHP, continue inside switch acts like break for the switch and triggers a warning. Use continue 2 to skip to the next loop iteration.

Frequently Asked Questions

Should I use switch or match in PHP 8?

Use match whenever you are mapping an input value directly to a result or function call. Use switch when a branch needs several multi-line statements.

Does match need a break statement?

No. match evaluates only the first matching arm and automatically stops without falling through.

What happens if no match condition matches and there is no default?

PHP throws an UnhandledMatchError exception, which helps catch missing enum or status cases during testing.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
PHP Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum