PHP switch statement with cases and default block
The PHP switch statement compares one expression against a list of case values and runs the first matching branch. Use it when a variable holds one of several fixed options and each option triggers different logic. Grouping multiple case labels (each a PHP case statement) into one branch keeps repeated conditions out of long if/elseif chains.
PHP Switch Statement Example For Value Matching
Output:
Output will appear here...
Output:
Write resource
How This Example Works
switch ($method)evaluates$methodonce and compares the result to eachcasevalue using loose comparison (==).case "GET"does not match"POST", so execution skips to the next case.case "POST"matches. Because nobreakfollows this label, execution falls through into thecase "PUT"block — this groups both methods under the same branch.breakexits the switch after printing. Without it, execution would continue intodefault.defaultacts as a catch-all when no case matches. It is optional but recommended for defensive code.
Does PHP Switch Use Strict or Loose Comparison?
PHP switch uses loose comparison (==), so values can be coerced during matching. A common gotcha is "0" matching case 0, and false matching case 0 as well. Case order matters: the first loosely-equal match wins. When strict comparison matters, use match (PHP 8+) or explicit if/elseif with ===.
Common Mistakes in PHP Switch
Missing break (accidental fall-through):
Wrong:
$role = "admin";
switch ($role) {
case "admin":
$label = "admin";
case "editor":
$label = "editor";
break;
}
Right:
$role = "admin";
switch ($role) {
case "admin":
$label = "admin";
break;
case "editor":
$label = "editor";
break;
}
Without a break, a matched case continues executing the subsequent branches. Intentional fall-through should group labels with no code between them.
Using continue inside a loop expecting to skip to the next iteration:
Wrong:
$items = ["keep", "skip", "keep"];
foreach ($items as $item) {
switch ($item) {
case "skip":
continue; // exits the switch, not the loop
}
echo $item . "\\n";
}
Right:
$items = ["keep", "skip", "keep"];
foreach ($items as $item) {
switch ($item) {
case "skip":
continue 2; // advances the foreach
}
echo $item . "\\n";
}
Inside a switch nested in a loop, continue behaves like break for the switch. Use continue 2 to target the outer loop.
PHP Switch vs Match Expression
| switch | match (PHP 8+) |
|---|---|
| Statement — does not return a value | Expression — returns a value |
Loose comparison (==) | Strict comparison (===) |
| Fall-through by default | No fall-through; each arm returns |
| Supports multi-line blocks per case | One expression per arm |
Use switch when branches contain multiple statements or when intentional fall-through groups cases. Use match when mapping a value to a result with strict type safety.
FAQ
Why is break needed in a PHP switch statement?
PHP switch uses fall-through by default: once a case matches, all subsequent branches execute until a break or the end of the switch block. The break statement exits the switch immediately. Omitting it is only safe when you intentionally group cases that share the same logic.
When should I use match instead of switch in PHP 8+?
Prefer match when you need strict comparison, want to assign the result to a variable, or have one expression per branch. Stick with switch when branches contain multiple statements, side effects, or deliberate fall-through across cases.