A switch statement helps you choose one action from many options based on a value. It makes your code easier to read than many if-else checks.
Switch statement execution in PHP
Introduction
Syntax
PHP
switch (expression) { case value1: // code to run if expression == value1 break; case value2: // code to run if expression == value2 break; default: // code to run if no case matches break; }
The break stops the switch from running the next cases.
The default case runs if no other case matches.
Examples
$day.PHP
switch ($day) { case 'Monday': echo "Start of the week"; break; case 'Friday': echo "Almost weekend"; break; default: echo "Just another day"; break; }
PHP
switch ($number) { case 1: case 2: case 3: echo "Small number"; break; default: echo "Other number"; break; }
Sample Program
This program checks the color and prints the matching traffic signal message.
PHP
<?php $color = 'green'; switch ($color) { case 'red': echo "Stop"; break; case 'yellow': echo "Get ready"; break; case 'green': echo "Go"; break; default: echo "Unknown color"; break; } ?>
Important Notes
Always use break to avoid running code in the next cases unintentionally.
The default case is optional but useful for unexpected values.
Switch works best when checking one variable against many fixed values.
Summary
Switch statements let you pick code to run based on a value.
Use case for each option and break to stop.
default runs if no cases match.
Practice
1. What does a
switch statement do in PHP?easy
Solution
Step 1: Understand the purpose of switch
A switch statement compares a value to multiple cases to decide which code block to run.Step 2: Compare with other options
Repeating code is done by loops, functions define reusable code, and variables store data, so these are not correct.Final Answer:
It chooses which code to run based on a value. -> Option AQuick Check:
Switch selects code by value = A [OK]
Hint: Switch picks code by matching value quickly [OK]
Common Mistakes:
- Confusing switch with loops
- Thinking switch defines functions
- Believing switch creates variables
2. Which of the following is the correct syntax to start a switch statement in PHP?
easy
Solution
Step 1: Recall switch syntax
In PHP, switch requires parentheses around the variable: switch (variable) {Step 2: Check other options
Options without parentheses or with brackets or parentheses after variable name are invalid syntax.Final Answer:
switch (variable) { -> Option CQuick Check:
Switch uses parentheses around variable [OK]
Hint: Switch always uses parentheses around the variable [OK]
Common Mistakes:
- Omitting parentheses
- Using square brackets instead of parentheses
- Adding parentheses after variable name
3. What will be the output of this PHP code?
$color = 'red';
switch ($color) {
case 'blue':
echo 'Blue color';
break;
case 'red':
echo 'Red color';
break;
default:
echo 'No color';
}medium
Solution
Step 1: Check the value of $color
$color is set to 'red'.Step 2: Match cases in switch
The switch checks 'blue' first (no match), then 'red' (match), so it runs echo 'Red color' and breaks.Final Answer:
Red color -> Option DQuick Check:
Value 'red' matches case 'red' [OK]
Hint: Match value to case, break stops further checks [OK]
Common Mistakes:
- Forgetting break causes fall-through
- Assuming default runs even if case matches
- Mixing case values
4. Identify the error in this PHP switch code:
$day = 3;
switch ($day) {
case 1:
echo 'Monday';
case 2:
echo 'Tuesday';
break;
default:
echo 'Other day';
}medium
Solution
Step 1: Check breaks in cases
Case 1 has no break, so if matched, it falls through to case 2.Step 2: Understand effect of missing break
Missing break causes code for case 2 to run even if case 1 matches, which is usually unintended.Final Answer:
Missing break after case 1 causes fall-through. -> Option AQuick Check:
Break missing after case 1 causes fall-through [OK]
Hint: Always add break after each case to avoid fall-through [OK]
Common Mistakes:
- Ignoring missing break causes multiple cases to run
- Thinking default must be first
- Assuming variable is undefined
5. Given this PHP code, what will be the output?
$score = 85;
switch (true) {
case ($score >= 90):
echo 'Grade A';
break;
case ($score >= 80):
echo 'Grade B';
break;
case ($score >= 70):
echo 'Grade C';
break;
default:
echo 'Fail';
}hard
Solution
Step 1: Understand switch(true) usage
The switch compares true to each case condition result.Step 2: Evaluate each case condition
$score is 85, so ($score >= 90) is false, ($score >= 80) is true, so it matches case 2 and prints 'Grade B'.Final Answer:
Grade B -> Option BQuick Check:
Switch(true) matches first true case = Grade B [OK]
Hint: Use switch(true) to check conditions in order [OK]
Common Mistakes:
- Expecting switch to compare values directly
- Not understanding switch(true) trick
- Missing break causing fall-through
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
