Switch statement execution in PHP - Time & Space Complexity
We want to understand how long a switch statement takes to run as the number of cases grows.
How does the time to find the matching case change when there are more options?
Analyze the time complexity of the following code snippet.
function getDayName(int $dayNumber): string {
switch ($dayNumber) {
case 1: return 'Monday';
case 2: return 'Tuesday';
case 3: return 'Wednesday';
case 4: return 'Thursday';
case 5: return 'Friday';
case 6: return 'Saturday';
case 7: return 'Sunday';
default: return 'Invalid day';
}
}
This code returns the name of the day based on a number using a switch statement.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Direct jump to the matching case via a jump table or hash lookup (compiler-optimized).
- How many times: Constant time, typically 1 operation regardless of cases.
As the number of cases increases, the time remains constant due to optimization.
| Input Size (number of cases) | Approx. Operations |
|---|---|
| 10 | ~1 jump |
| 100 | ~1 jump |
| 1000 | ~1 jump |
Pattern observation: Time stays constant; compiler uses jump tables for dense integers.
Time Complexity: O(1)
This means the time to find the matching case is constant, independent of the number of cases.
[X] Wrong: "Switch statements check cases one by one, so O(n)."
[OK] Correct: Compilers optimize switches on integers to jump tables or binary search/hashes, making it O(1) average/worst case for typical use.
Understanding switch optimizations shows deep knowledge of low-level compiler behavior and why simple structures can be efficient at scale.
"What if the switch used strings or sparse values? How might complexity change?"
Practice
switch statement do in PHP?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]
- Confusing switch with loops
- Thinking switch defines functions
- Believing switch creates variables
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]
- Omitting parentheses
- Using square brackets instead of parentheses
- Adding parentheses after variable name
$color = 'red';
switch ($color) {
case 'blue':
echo 'Blue color';
break;
case 'red':
echo 'Red color';
break;
default:
echo 'No color';
}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]
- Forgetting break causes fall-through
- Assuming default runs even if case matches
- Mixing case values
$day = 3;
switch ($day) {
case 1:
echo 'Monday';
case 2:
echo 'Tuesday';
break;
default:
echo 'Other day';
}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]
- Ignoring missing break causes multiple cases to run
- Thinking default must be first
- Assuming variable is undefined
$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';
}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]
- Expecting switch to compare values directly
- Not understanding switch(true) trick
- Missing break causing fall-through
Ready to prove it?
Pass the test and this pattern moves unknown
