PHP Switch Statement
The switch statement is a cleaner alternative to writing long chains of if-elseif-else statements when a single variable needs to be compared against multiple specific values. Instead of repeating the same variable in every condition, the switch statement evaluates the variable once and then checks which case matches its value.
Basic Switch Syntax
<?php
$day = "Tuesday";
switch ($day) {
case "Monday":
echo "Start of the work week.";
break;
case "Tuesday":
echo "Second day of the week.";
break;
case "Wednesday":
echo "Midweek.";
break;
case "Friday":
echo "Almost the weekend!";
break;
default:
echo "Just another day.";
}
// Outputs: Second day of the week.
?>
PHP compares the value of $day with each case label from top to bottom. When a match is found, the code for that case runs. The break statement stops execution and jumps out of the switch block. The default case runs when no other case matches — it acts like the final else in an if-elseif chain.
The Role of break
The break statement is essential. Without it, PHP continues executing every case below the matching one — a behavior called fall-through.
<?php
$color = "blue";
switch ($color) {
case "red":
echo "Red chosen.";
case "blue":
echo "Blue chosen."; // This runs
case "green":
echo "Green chosen."; // This ALSO runs (fall-through without break)
default:
echo "Default case."; // This ALSO runs
}
// Outputs: Blue chosen. Green chosen. Default case.
?>
The fall-through here is unintentional. Adding break after each case prevents this.
<?php
$color = "blue";
switch ($color) {
case "red":
echo "Red chosen.";
break;
case "blue":
echo "Blue chosen."; // Outputs: Blue chosen.
break;
case "green":
echo "Green chosen.";
break;
default:
echo "Unknown color.";
}
?>
Intentional Fall-Through
Sometimes fall-through is useful. Multiple cases can share the same block of code by omitting break between them intentionally.
<?php
$month = 4; // April
switch ($month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
echo "This month has 31 days.";
break;
case 4:
case 6:
case 9:
case 11:
echo "This month has 30 days.";
break;
case 2:
echo "February has 28 or 29 days.";
break;
}
// Outputs: This month has 30 days.
?>
Months 4, 6, 9, and 11 all fall through to the same block, which is exactly the intended behavior.
Switch with Strings
<?php
$status = "pending";
switch ($status) {
case "active":
echo "Account is active.";
break;
case "pending":
echo "Account is awaiting approval.";
break;
case "suspended":
echo "Account has been suspended.";
break;
case "closed":
echo "Account is permanently closed.";
break;
default:
echo "Unknown account status.";
}
// Outputs: Account is awaiting approval.
?>
Switch with return Inside a Function
When a switch statement is inside a function, return can be used instead of break to exit both the case and the function.
<?php
function getSeasonName($month) {
switch ($month) {
case 12:
case 1:
case 2:
return "Winter";
case 3:
case 4:
case 5:
return "Spring";
case 6:
case 7:
case 8:
return "Summer";
case 9:
case 10:
case 11:
return "Autumn";
default:
return "Unknown season";
}
}
echo getSeasonName(7); // Outputs: Summer
echo getSeasonName(11); // Outputs: Autumn
?>
Switch vs if-elseif — When to Use Each
| Situation | Recommended |
|---|---|
| Comparing a single variable to many fixed values | switch |
| Checking ranges (e.g., score >= 90) | if-elseif |
| Complex conditions with multiple variables | if-elseif |
| Multiple cases sharing the same action | switch (with fall-through) |
PHP 8 — The match Expression
PHP 8 introduced the match expression as a modern, stricter alternative to switch. It uses strict comparison (===) instead of loose comparison (==), has no fall-through, and returns a value directly.
<?php
$statusCode = 404;
$message = match($statusCode) {
200 => "OK",
301 => "Moved Permanently",
404 => "Not Found",
500 => "Internal Server Error",
default => "Unknown Status"
};
echo $message; // Outputs: Not Found
?>
The match expression is cleaner and safer than switch for returning values. It throws an error if no arm matches and there is no default — which helps catch bugs early.
Key Points
- The switch statement compares one expression against multiple case values.
- Each case must end with
breakto prevent fall-through to the next case. - The
defaultcase handles any value that does not match an explicit case. - Intentional fall-through allows multiple cases to share one code block.
- Switch uses loose comparison (
==) rather than strict comparison (===). - PHP 8's
matchexpression is a stricter, no-fall-through alternative that returns values directly.
