Bird
0
0

You want to assign a category based on age using an elseif ladder. Which code correctly assigns "Child" for age < 13, "Teen" for age between 13 and 19 inclusive, and "Adult" for age 20 or more?

hard📝 Application Q15 of 15
PHP - Conditional Statements

You want to assign a category based on age using an elseif ladder. Which code correctly assigns "Child" for age < 13, "Teen" for age between 13 and 19 inclusive, and "Adult" for age 20 or more?

<?php
$age = 15;
if ($age < 13) {
    $category = "Child";
} elseif ($age >= 13 && $age <= 19) {
    $category = "Teen";
} else {
    $category = "Adult";
}
echo $category;
?>
ASyntax error due to missing elseif keyword
BCorrectly assigns categories as required
CAssigns "Adult" for age 19 incorrectly
DAssigns "Child" for age 13 incorrectly
Step-by-Step Solution
Solution:
  1. Step 1: Check age ranges in conditions

    First condition covers age less than 13, second covers 13 to 19 inclusive, else covers 20 or more.
  2. Step 2: Verify assignment and output

    Each category is assigned correctly based on age, and echo prints the category.
  3. Final Answer:

    Correctly assigns categories as required -> Option B
  4. Quick Check:

    Age ranges match categories perfectly [OK]
Quick Trick: Use && to combine conditions in elseif [OK]
Common Mistakes:
  • Overlapping or missing age ranges
  • Using else if instead of elseif in PHP
  • Forgetting to echo the category

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes