Bird
0
0

You want to print "Adult" if age is 18 or more, "Minor" if less than 18, and "Invalid" if age is negative. Which code correctly uses if-else to do this?

hard📝 Application Q8 of 15
PHP - Conditional Statements
You want to print "Adult" if age is 18 or more, "Minor" if less than 18, and "Invalid" if age is negative. Which code correctly uses if-else to do this?
Aif ($age >= 18) { echo "Adult"; } else if ($age < 18) { echo "Minor"; } else { echo "Invalid"; }
Bif ($age >= 18) { echo "Adult"; } else { if ($age < 0) { echo "Minor"; } else { echo "Invalid"; } }
Cif ($age < 0) { echo "Invalid"; } else { if ($age <= 18) { echo "Adult"; } else { echo "Minor"; } }
Dif ($age < 0) { echo "Invalid"; } elseif ($age >= 18) { echo "Adult"; } else { echo "Minor"; }
Step-by-Step Solution
Solution:
  1. Step 1: Check order of conditions for correct logic

    Invalid age (negative) must be checked first to avoid wrong classification.
  2. Step 2: Verify each option's condition order and output

    if ($age < 0) { echo "Invalid"; } elseif ($age >= 18) { echo "Adult"; } else { echo "Minor"; } checks negative first, then adult, else minor. Others check adult first or mix order causing wrong output.
  3. Final Answer:

    if ($age < 0) { echo "Invalid"; } elseif ($age >= 18) { echo "Adult"; } else { echo "Minor"; } -> Option D
  4. Quick Check:

    Check invalid first, then adult, else minor [OK]
Quick Trick: Check invalid conditions before others in if-else chains [OK]
Common Mistakes:
  • Checking adult before invalid causes wrong output
  • Using else if instead of elseif (minor difference but valid)
  • Nesting if unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes