Bird
Raised Fist0
PHPprogramming~20 mins

Switch statement execution in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Switch Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of switch with fall-through
What is the output of this PHP code?
PHP
<?php
$val = 2;
switch ($val) {
    case 1:
        echo "One ";
    case 2:
        echo "Two ";
    case 3:
        echo "Three ";
        break;
    default:
        echo "Default ";
}
?>
AOne Two Three
BTwo Three
CTwo
DDefault
Attempts:
2 left
💡 Hint
Remember that without break, cases fall through to the next.
❓ Predict Output
intermediate
2:00remaining
Switch with strict type comparison
What will this PHP code output?
PHP
<?php
$value = '5';
switch ($value) {
    case 5:
        echo "Number 5";
        break;
    case '5':
        echo "String 5";
        break;
    default:
        echo "No match";
}
?>
ANumber 5
BString 5
CNo match
DError
Attempts:
2 left
💡 Hint
PHP switch uses loose comparison (==), not strict (===).
🔧 Debug
advanced
2:00remaining
Identify the error in switch syntax
Which option contains a syntax error in the switch statement?
A
&lt;?php
switch ($x) {
    case 1:
        echo "One";
        break;
    default
        echo "Default";
}
?&gt;
B
&lt;?php
switch ($x) {
    case 1:
        echo "One";
        break;
    default:
        echo "Default";
}
?&gt;
C
&lt;?php
switch ($x) {
    case 1:
        echo "One";
        break;
    case 2:
        echo "Two";
        break;
}
?&gt;
D
&lt;?php
switch ($x) {
    case 1:
        echo "One";
        break;
    case 2:
        echo "Two";
        break;
    default:
        echo "Default";
}
?&gt;
Attempts:
2 left
💡 Hint
Check the syntax for the default case.
❓ Predict Output
advanced
2:00remaining
Switch with multiple cases and no breaks
What is the output of this PHP code?
PHP
<?php
$val = 1;
switch ($val) {
    case 0:
    case 1:
    case 2:
        echo "Low ";
    case 3:
        echo "Mid ";
        break;
    default:
        echo "High ";
}
?>
AMid
BLow
CLow Mid
DHigh
Attempts:
2 left
💡 Hint
Cases 0,1,2 share the same block without breaks.
🧠 Conceptual
expert
2:00remaining
Behavior of switch with non-scalar expression
What happens when you use an array as the switch expression in PHP like this?
PHP
<?php
$arr = [1, 2];
switch ($arr) {
    case [1, 2]:
        echo "Match";
        break;
    default:
        echo "No match";
}
?>
AOutputs 'Match' because arrays are compared by value.
BOutputs 'No match' because switch cannot compare arrays.
CCauses a fatal error due to invalid switch expression.
DRaises a warning and outputs 'No match'.
Attempts:
2 left
💡 Hint
PHP switch uses loose comparison but arrays in switch cause warnings.

Practice

(1/5)
1. What does a switch statement do in PHP?
easy
A. It chooses which code to run based on a value.
B. It repeats code multiple times.
C. It defines a function.
D. It creates a new variable.

Solution

  1. Step 1: Understand the purpose of switch

    A switch statement compares a value to multiple cases to decide which code block to run.
  2. 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.
  3. Final Answer:

    It chooses which code to run based on a value. -> Option A
  4. Quick 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
A. switch [variable] {
B. switch variable {
C. switch (variable) {
D. switch variable() {

Solution

  1. Step 1: Recall switch syntax

    In PHP, switch requires parentheses around the variable: switch (variable) {
  2. Step 2: Check other options

    Options without parentheses or with brackets or parentheses after variable name are invalid syntax.
  3. Final Answer:

    switch (variable) { -> Option C
  4. Quick 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
A. Blue color
B. Syntax error
C. No color
D. Red color

Solution

  1. Step 1: Check the value of $color

    $color is set to 'red'.
  2. Step 2: Match cases in switch

    The switch checks 'blue' first (no match), then 'red' (match), so it runs echo 'Red color' and breaks.
  3. Final Answer:

    Red color -> Option D
  4. Quick 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
A. Missing break after case 1 causes fall-through.
B. Switch statement syntax is incorrect.
C. Variable $day is not defined.
D. Default case should be first.

Solution

  1. Step 1: Check breaks in cases

    Case 1 has no break, so if matched, it falls through to case 2.
  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.
  3. Final Answer:

    Missing break after case 1 causes fall-through. -> Option A
  4. Quick 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
A. Grade A
B. Grade B
C. Grade C
D. Fail

Solution

  1. Step 1: Understand switch(true) usage

    The switch compares true to each case condition result.
  2. 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'.
  3. Final Answer:

    Grade B -> Option B
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard