Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Conditional Statements

What will be the output of this PHP code?

<?php
$age = 20;
if ($age > 60) {
    echo "Senior";
} elseif ($age >= 18) {
    echo "Adult";
} else {
    echo "Minor";
}
AMinor
BSenior
CAdult
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Check the value of $age and conditions

    $age is 20. First condition checks if greater than 60 (false). Second checks if greater or equal 18 (true).
  2. Step 2: Determine which block runs

    Since second condition is true, it prints "Adult" and skips the else block.
  3. Final Answer:

    Adult -> Option C
  4. Quick Check:

    First true condition runs: $age >= 18 [OK]
Quick Trick: Check conditions top to bottom, first true runs [OK]
Common Mistakes:
  • Choosing 'Senior' because 20 is less than 60
  • Ignoring elseif and picking else
  • Thinking multiple blocks print

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes