Bird
0
0

Identify the error in this PHP switch code:

medium📝 Debug Q14 of 15
PHP - Conditional Statements
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';
 }
AMissing break after case 1 causes fall-through.
BSwitch statement syntax is incorrect.
CVariable $day is not defined.
DDefault case should be first.
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes