Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - Conditional Statements
What will be the output of this PHP code?
$value = 'b';
switch ($value) {
  case 'a':
    echo "Apple";
  case 'b':
    echo "Banana";
  case 'c':
    echo "Cherry";
    break;
  default:
    echo "No fruit";
}
ANo fruit
BAppleBananaCherry
CBanana
DBananaCherry
Step-by-Step Solution
Solution:
  1. Step 1: Find matching case

    The variable $value is 'b', so it matches case 'b'.
  2. Step 2: Understand fall-through without break

    Since case 'b' has no break, it executes echo "Banana" and continues to case 'c', echoing "Cherry" before breaking.
  3. Final Answer:

    BananaCherry -> Option D
  4. Quick Check:

    Switch without break falls through [OK]
Quick Trick: No break causes fall-through to next cases [OK]
Common Mistakes:
  • Expecting only Banana output
  • Assuming break after each case automatically
  • Choosing default output incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes