PHP Switch
When you compare one value against many possible options, a long if/elseif chain becomes repetitive. The switch statement expresses the same idea more directly: evaluate an expression once, then jump to the case that matches.
Basic syntax
switch (expression) {
case value1:
// code when expression == value1
break;
case value2:
// code when expression == value2
break;
default:
// code when nothing matched
}- Each
caseis compared to the expression using loose comparison (==). breakleaves the switch. Without it, execution "falls through" into the next case.defaultruns when no case matches. It is optional and usually placed last.
<?php
$day = "Sat";
switch ($day) {
case "Mon":
echo "Start of the work week\n";
break;
case "Fri":
echo "Almost weekend!\n";
break;
case "Sat":
echo "Weekend: time for a hike\n";
break;
default:
echo "A regular weekday\n";
}Weekend: time for a hike
Fall-through: grouping cases
Forgetting break is a classic bug, but fall-through is also useful on purpose: stack several case labels to share one block of code.
<?php
function dayType(string $day): string {
switch ($day) {
case "Sat":
case "Sun":
return "weekend";
case "Mon":
case "Tue":
case "Wed":
case "Thu":
case "Fri":
return "weekday";
default:
return "unknown";
}
}
foreach (["Sun", "Wed", "Funday"] as $d) {
echo "$d is a " . dayType($d) . "\n";
}Sun is a weekend
Wed is a weekday
Funday is a unknown
Inside a function, return leaves both the switch and the function, so no break is needed.
What happens without break
Here is the accidental version. Once a case matches, PHP keeps running every following statement until it reaches a break or the end of the switch:
<?php
$level = 2;
switch ($level) {
case 1:
echo "Unlocked: bronze badge\n";
case 2:
echo "Unlocked: silver badge\n";
case 3:
echo "Unlocked: gold badge\n";
break;
case 4:
echo "Unlocked: platinum badge\n";
}Unlocked: silver badge
Unlocked: gold badge
Sometimes this is exactly what you want (a level-2 player earns every badge up to gold), but if it is intentional, leave a // fall through comment so readers know it isn't a mistake.
Loose comparison pitfalls
Because switch uses ==, values of different types may match. In PHP 8, 0 == "a" is false, but "1" == "01" and 100 == "1e2" are still true, because both sides are numeric strings or numbers.
<?php
$input = "1e2"; // a numeric string, e.g. from a form
switch ($input) {
case 100:
echo "Matched the integer 100\n";
break;
default:
echo "No match\n";
}Matched the integer 100
If you need strict comparison, use the match expression (next lesson) or a switch (true) with explicit === checks.
switch (true)
A common trick is to switch on true and write full conditions as cases. The first case whose condition evaluates to true wins:
switch (true) {
case $bmi < 18.5: $label = "underweight"; break;
case $bmi < 25: $label = "normal"; break;
default: $label = "overweight";
}switch vs if
Use switch when… |
Use if/elseif when… |
|---|---|
| You compare one value to fixed options | Conditions involve different variables |
| Several options share the same code | You need ranges like > 10 && < 20 |
| You want a readable list of cases | You need strict === comparison |
Tip: In modern PHP,
matchis often a better fit thanswitchwhen each case just produces a value. Keepswitchfor cases that run several statements.
Test yourself
What comparison does switch use to test each case?
- Strict
=== - Loose
== - Spaceship
<=> - Pattern matching
What happens when a matching case has no break?
- PHP throws an error
- The switch restarts
- Execution falls through into the next case
- Only the default runs
When does the default block run?
- Always, at the end
- When no case matched
- When the first case matched
- Only if it is written first