PHP Switch Statement: A Comprehensive Guide
The PHP switch statement is a useful tool for controlling the flow of code in a program. This statement allows you to test a single expression against multiple
The PHP switch statement compares one expression against a list of possible values and runs the block that matches. It is the cleaner alternative to a long chain of if...elseif...else statements when you are testing the same variable against many fixed values.
This page covers the syntax, how matching and fall-through actually work, real runnable examples, the PHP 8 match alternative, and the gotchas that trip people up.
Syntax
switch (expression) {
case value1:
// runs when expression == value1
break;
case value2:
// runs when expression == value2
break;
default:
// runs when nothing above matched
}The expression is evaluated once. Its result is then compared, top to bottom, against each case value using loose comparison (==). On the first match, PHP starts running statements from that case and keeps going until it hits a break (or the end of the switch). The optional default block runs when no case matched — by convention it goes last.
A working example
This example maps a day number to a name and prints the result:
<?php
$day = 3;
switch ($day) {
case 1:
echo "Monday";
break;
case 2:
echo "Tuesday";
break;
case 3:
echo "Wednesday";
break;
default:
echo "Another day";
}
// Output: WednesdayBecause $day is 3, the third case matches, "Wednesday" is printed, and break stops execution before the default block.
Why break matters: fall-through
If you forget a break, execution "falls through" into the next case and keeps running until it finds one. This is the single most common switch bug:
<?php
$role = "editor";
switch ($role) {
case "editor":
echo "Can edit. ";
// no break — falls through!
case "viewer":
echo "Can view.";
break;
}
// Output: Can edit. Can view.Here "editor" matched, but with no break PHP also ran the viewer block.
Intentional fall-through (grouping cases)
Fall-through is occasionally useful on purpose: stacking empty cases lets several values share one block.
<?php
$letter = "e";
switch ($letter) {
case "a":
case "e":
case "i":
case "o":
case "u":
echo "Vowel";
break;
default:
echo "Consonant";
}
// Output: VowelSwitching on a condition with true
A switch can also replace an if...elseif ladder for range checks. Switch on true, and put a boolean expression in each case:
<?php
$score = 82;
switch (true) {
case $score >= 90:
echo "A";
break;
case $score >= 80:
echo "B";
break;
case $score >= 70:
echo "C";
break;
default:
echo "F";
}
// Output: BThe first case whose expression equals true wins, so order matters — list the tightest condition first.
switch vs match (PHP 8+)
PHP 8 added the match expression, a stricter sibling of switch. Reach for match when you simply map a value to a result:
<?php
$status = 404;
$message = match ($status) {
200, 201 => "Success",
404 => "Not Found",
500 => "Server Error",
default => "Unknown",
};
echo $message;
// Output: Not FoundKey differences:
switch | match | |
|---|---|---|
| Comparison | loose (==) | strict (===) |
| Fall-through | yes (needs break) | none |
| Returns a value | no | yes (it's an expression) |
Unmatched value, no default | does nothing | throws UnhandledMatchError |
Loose comparison gotcha
Because switch uses ==, a numeric 0 can match a non-empty string in older code. In PHP 8+ string-to-number comparison rules were tightened, but keep types consistent to avoid surprises:
<?php
$value = 0;
switch ($value) {
case "hello":
echo "matched hello";
break;
default:
echo "no match";
}
// Output (PHP 8+): no matchOn PHP 7, the same code would have printed matched hello because 0 == "hello" was true. When in doubt, use match for strict comparison.
Best practices
- Always add
breakafter each case unless you deliberately want fall-through (and comment it when you do). - Include a
defaultto handle unexpected values instead of silently doing nothing. - Group related cases by stacking empty
caselabels rather than duplicating code. - Prefer
match(PHP 8+) when you are returning a value or need strict comparison. - Use
if...elseifwhen you are testing different expressions, not one value against many.
Related topics
- PHP If...Else...Elseif — conditional branching
- PHP Operators — comparison and the
matchexpression - PHP Loops — repeating blocks of code