Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q13 of 15
PHP - Conditional Statements
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';
 }
ABlue color
BSyntax error
CNo color
DRed color
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes