PHP Switch
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
Run
php -vto confirm your PHP version.matchneeds 8.0 or newer, and older versions give a parse error.Test every branch, including
default, with at least one input each.Try awkward inputs: a number as a string (
"404"vs404),null, an empty string, and an unexpected value.For
match, remove thedefaultarm temporarily and confirm anUnhandledMatchErroris thrown for an unknown input.Use
var_dump()on the result to see the type, not just the printed text.
Common Mistakes to Avoid
Forgetting
breakinswitch: Withoutbreak, PHP continues executing every followingcaseblock until it hits abreakor the closing brace.Relying on
switchloose equality with0or"":switch (0)loosely matchescase "guest":in older PHP versions or unexpected numeric strings. Usematchfor strict type checks.Using
continueinside aswitchinside aloop: In PHP,continueinsideswitchacts likebreakfor theswitchand triggers a warning. Usecontinue 2to 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.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.