switch ($value) compares one value against a list of case labels and runs the code under the first label that matches. Each case ends with break; default handles everything that matched nothing.
Set $day to 'fri', 'sun' or 'wed' and run it again. The default case does not need a break when it is the last one.
Multiple cases for one block
To run the same code for several values, stack the case labels with nothing between them. A matching label continues into the code below it.
Unlike match, a switch has no case 'jpg', 'jpeg': comma syntax. Stacked labels are the only way.
Fallthrough: what happens without break
break is what stops a switch. Without it, PHP runs the matching case and then keeps going into every case below, ignoring their labels, until it hits a break or the end.
Here the fallthrough is on purpose: each plan gets its own features plus everything from the cheaper plans. When you write it deliberately, add a comment such as // no break so the next reader does not "fix" it. When you did not mean it, a missing break is one of the most common switch bugs.
switch uses loose comparison (==)
Every case is checked with ==, not ===. A numeric string matches a number, null matches false and "", and true matches any value that counts as true. Tap the cells to compare the two operators.
| $a == $b | 0 | 1 | "0" | "1" | "" | null | false | true | "a" | "10" | "1e1" |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | |||||||||||
| 1 | |||||||||||
| "0" | |||||||||||
| "1" | |||||||||||
| "" | |||||||||||
| null | |||||||||||
| false | |||||||||||
| true | |||||||||||
| "a" | |||||||||||
| "10" | |||||||||||
| "1e1" |
The practical consequence: an input string "1" from a form lands in case 1: without any conversion, which is often what you want. But switch (true) with a non-empty string, or null meeting case false:, can pick a case you did not expect.
Note the null line: it never reaches case false:, because null == 0 is already true and case 0: comes first. With loose comparison, the order of the cases changes the result. Since PHP 8, 0 == "a" is false (in PHP 7 it was true), so a non-numeric string no longer matches case 0:. If you need ===, use a match expression, covered at the end of this page.
switch(true) for ranges and conditions
A case label can be any expression. With switch (true), each case is a condition, and the first one that equals true wins.
Make sure every case is a real boolean. Because of loose comparison, case $name: would match whenever $name is a non-empty string.
switch inside a loop: break and continue
Inside a loop, break in a switch only leaves the switch, not the loop. To stop the loop, use break 2. continue inside a switch behaves like break, and PHP warns at compile time: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"?. Write continue 2 to skip to the next loop iteration.
switch vs match
PHP 8 added match, which covers most uses of switch with less code. It compares with ===, returns a value, never falls through, allows comma separated conditions, and throws an UnhandledMatchError when nothing matches, so a forgotten case cannot pass silently.
Keep switch when a case needs several statements, or when fallthrough is the point, as in the plan example. For mapping a value to a result, match is shorter and safer.
Frequently Asked Questions
How do I use multiple cases for one block in a PHP switch?
Stack the case labels with no code between them: case 'sat': case 'sun': echo 'weekend'; break;. Each label falls through to the shared code. PHP has no case 'sat', 'sun': syntax for switch; that comma form belongs to match.
Does PHP switch use strict comparison?
No. switch compares with loose ==, so case '1': matches the integer 1 and case 0: matches '0'. For strict === comparison, use a match expression or switch (true) with === inside each case.
What happens if I forget break in a PHP switch?
Execution falls through: after the matching case, PHP keeps running the code of every case below it until it reaches a break, a return or the end of the switch. Sometimes that is intended, but usually it is a bug.
Can a PHP switch check ranges like greater than?
Yes, with switch (true): each case holds a condition such as case $score >= 90:, and the first one that is true runs. A match (true) expression does the same with strict comparison and returns a value.