Bird
0
0

Given this PHP code, what will be the output?

hard📝 Application Q15 of 15
PHP - Conditional Statements
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';
 }
AGrade A
BGrade B
CGrade C
DFail
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes