Switch Statement
Part of the Fundamentals section of Coddy's PHP journey — lesson 32 of 71.
When you need to compare a single variable against many specific values, the switch statement offers a cleaner alternative to long if-elseif chains.
The switch statement checks a variable once and then matches it against different case values:
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the week";
break;
case "Friday":
echo "Almost weekend";
break;
case "Saturday":
case "Sunday":
echo "Weekend!";
break;
default:
echo "Regular day";
}
?>PHP compares $day against each case. When it finds a match, it runs that block's code.
The break statement is crucial—it tells PHP to exit the switch after executing the matched case. Without it, PHP would continue running all the code below, even from other cases.
Notice how Saturday and Sunday share the same code by stacking cases together. The default case acts like an else—it runs when no other case matches.
<?php
$grade = "B";
switch ($grade) {
case "A":
echo "Excellent!";
break;
case "B":
echo "Good job!";
break;
default:
echo "Keep trying!";
}
?>Use switch when comparing one variable against multiple exact values. For complex conditions or ranges, stick with if-elseif statements.
Challenge
EasyRead a string command from input representing a traffic light color.
Use a switch statement to determine the appropriate action based on the color:
- If the command is
red, printStop - If the command is
yellow, printSlow down - If the command is
green, printGo - If the command is
flashing redorflashing yellow, printProceed with caution - For any other value, print
Invalid signal
Example:
If the input is green, the output should be:
GoIf the input is flashing red, the output should be:
Proceed with cautionIf the input is blue, the output should be:
Invalid signalCheat sheet
The switch statement provides a cleaner way to compare a single variable against multiple specific values:
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the week";
break;
case "Friday":
echo "Almost weekend";
break;
default:
echo "Regular day";
}
?>The break statement exits the switch after executing the matched case. Without it, PHP continues running code from subsequent cases.
Multiple cases can share the same code by stacking them:
<?php
switch ($day) {
case "Saturday":
case "Sunday":
echo "Weekend!";
break;
}
?>The default case runs when no other case matches, similar to an else statement.
Use switch statements for comparing one variable against multiple exact values. For complex conditions or ranges, use if-elseif statements instead.
Try it yourself
<?php
// Read input
$command = trim(fgets(STDIN));
// TODO: Write your code below using a switch statement
// Determine the appropriate action based on the traffic light color
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators