Bird
0
0

How can you use nested conditional execution to categorize a person's age group as 'Child', 'Teen', or 'Adult' in PHP?Choose the correct code snippet.

hard📝 Application Q9 of 15
PHP - Conditional Statements

How can you use nested conditional execution to categorize a person's age group as 'Child', 'Teen', or 'Adult' in PHP?

Choose the correct code snippet.

A<?php $age = 16; if ($age < 13) { echo 'Child'; } else if ($age >= 20) { echo 'Adult'; } else { echo 'Teen'; } ?>
B<?php $age = 16; if ($age < 13) { echo 'Child'; } elseif ($age < 20) { echo 'Teen'; } else { echo 'Adult'; } ?>
C<?php $age = 16; if ($age < 13) { echo 'Child'; } if ($age < 20) { echo 'Teen'; } else { echo 'Adult'; } ?>
D<?php $age = 16; if ($age < 13) { echo 'Child'; } else { if ($age < 20) { echo 'Teen'; } else { echo 'Adult'; } } ?>
Step-by-Step Solution
Solution:
  1. Step 1: Understand nested if for age groups

    uses nested if inside else to separate 'Teen' and 'Adult' correctly.
  2. Step 2: Compare other options

    uses elseif (not nested), C prints multiple outputs, D changes order.
  3. Final Answer:

    Correctly uses nested if for age categorization -> Option D
  4. Quick Check:

    Nested if separates categories stepwise [OK]
Quick Trick: Use nested if inside else for multi-level checks [OK]
Common Mistakes:
  • Using multiple ifs causing multiple outputs
  • Confusing elseif with nested if
  • Incorrect age range checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes