PHP switch Statement
In this page:
switch ($variable) {
case value1:
// ...
break;
case value2:
// ...
break;
default:
// no case matched
}
Basic switch Statement
switch compares one variable against a list of fixed candidate values, which reads more cleanly than an equivalent elseif ladder once you're checking more than two or three specific values against the same variable.
Example: Basic switch Statement
<?php
// Declare `$day`, set to "Tuesday"
$day = "Tuesday";
// Switch on `$day` and branch by matching case
switch ($day) {
// Case: `"Monday"`
case "Monday":
// Print "Start of week" to the output
echo "Start of week";
break;
// Case: `"Tuesday"`
case "Tuesday":
// Print "Second day" to the output
echo "Second day";
break;
}
?>
Login to try C/C++/Java/PHP code in the editor
The Importance of break
Without an explicit break, execution 'falls through' into the next case's code after a match is found, running it too even though its own condition was never checked — a frequent source of bugs, and the main reason break is treated as mandatory at the end of nearly every case.
Example: The Importance of break
<?php
// Declare `$day`, set to "Monday"
$day = "Monday";
// Switch on `$day` and branch by matching case
switch ($day) {
// Case: `"Monday"`
case "Monday":
// Print "Monday\n" to the output
echo "Monday\n";
// Case: `"Tuesday"`
case "Tuesday":
// Print "Tuesday (falls through!)\n" to the output
echo "Tuesday (falls through!)\n";
break;
}
?>
Login to try C/C++/Java/PHP code in the editor
Default Switch Case
default runs when the tested value doesn't match any listed case — functionally the same role else plays at the end of an elseif ladder, catching whatever wasn't explicitly handled above it.
Example: Default Switch Case
<?php
// Declare `$fruit`, set to "mango"
$fruit = "mango";
// Switch on `$fruit` and branch by matching case
switch ($fruit) {
// Case: `"apple"`
case "apple":
// Print "Apple" to the output
echo "Apple";
break;
// Default case, if nothing above matched
default:
// Print "Unknown fruit" to the output
echo "Unknown fruit";
}
?>
Login to try C/C++/Java/PHP code in the editor
Grouping Switch Cases
Listing several case labels back-to-back with no code between them, followed by a single shared block, lets multiple distinct values trigger identical behavior without duplicating that block for each one.
Example: Grouping Switch Cases
<?php
// Declare `$day`, set to "Saturday"
$day = "Saturday";
// Switch on `$day` and branch by matching case
switch ($day) {
// Case: `"Saturday"`
case "Saturday":
// Case: `"Sunday"`
case "Sunday":
// Print "Weekend" to the output
echo "Weekend";
break;
// Default case, if nothing above matched
default:
// Print "Weekday" to the output
echo "Weekday";
}
?>
Login to try C/C++/Java/PHP code in the editor
Alternative Colon Syntax
Like if, switch supports a colon-based alternative syntax closed with endswitch;, which keeps templates that mix PHP and HTML more readable than the brace-heavy default form would.
Example: Alternative Colon Syntax
<?php
// Declare `$role`, set to "admin"
$role = "admin";
// Switch on `$role` and branch by matching case
switch ($role):
// Case: `"admin"`
case "admin":
// Print "Admin panel" to the output
echo "Admin panel";
break;
// Default case, if nothing above matched
default:
// Print "No access" to the output
echo "No access";
endswitch;
?>
Login to try C/C++/Java/PHP code in the editor
- Forgetting
break, so execution falls through and runs the following case's code as well. - Expecting
switchto compare strictly, when it uses==, so"0"and0can match the same case. - Leaving out
default, so unexpected values silently do nothing.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: