Bird
Raised Fist0
PHPprogramming~20 mins

Elseif ladder execution in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Elseif Ladder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of elseif ladder with numeric comparison
What is the output of this PHP code snippet?
PHP
<?php
$score = 75;
if ($score >= 90) {
    echo "Grade A";
} elseif ($score >= 80) {
    echo "Grade B";
} elseif ($score >= 70) {
    echo "Grade C";
} else {
    echo "Grade F";
}
?>
AGrade A
BGrade B
CGrade C
DGrade F
Attempts:
2 left
💡 Hint
Check which condition matches the score 75 first in the elseif ladder.
❓ Predict Output
intermediate
2:00remaining
Output when all elseif conditions fail
What will this PHP code output?
PHP
<?php
$temperature = 10;
if ($temperature > 30) {
    echo "Hot";
} elseif ($temperature > 20) {
    echo "Warm";
} elseif ($temperature > 15) {
    echo "Mild";
} else {
    echo "Cold";
}
?>
AWarm
BCold
CMild
DHot
Attempts:
2 left
💡 Hint
Check if 10 is greater than any of the conditions.
🔧 Debug
advanced
2:00remaining
Identify the error in elseif ladder syntax
What error will this PHP code produce?
PHP
<?php
$number = 5;
if ($number > 10) {
    echo "Greater than 10";
} elseif ($number > 5) {
    echo "Greater than 5";
} elseif $number > 0 {
    echo "Positive number";
} else {
    echo "Zero or negative";
}
?>
AWarning: Variable $number undefined
BNo output, code runs fine
CFatal error: Uncaught Error: Call to undefined function elseif()
DParse error: syntax error, unexpected '$number' (T_VARIABLE)
Attempts:
2 left
💡 Hint
Check the syntax of the elseif statement on line 5.
❓ Predict Output
advanced
2:00remaining
Output of elseif ladder with string comparison
What is the output of this PHP code?
PHP
<?php
$color = "green";
if ($color == "red") {
    echo "Stop";
} elseif ($color == "yellow") {
    echo "Caution";
} elseif ($color == "green") {
    echo "Go";
} else {
    echo "Unknown color";
}
?>
AGo
BStop
CCaution
DUnknown color
Attempts:
2 left
💡 Hint
Check which color matches the variable value.
❓ Predict Output
expert
2:00remaining
Number of times elseif ladder executes echo
How many times will the echo statement run in this PHP code?
PHP
<?php
$count = 0;
$value = 3;
if ($value > 5) {
    echo "A";
    $count++;
} elseif ($value > 2) {
    echo "B";
    $count++;
} elseif ($value > 0) {
    echo "C";
    $count++;
} else {
    echo "D";
    $count++;
}
echo " Count: $count";
?>
AEcho runs 2 times
BEcho runs 3 times
CEcho runs 1 time
DEcho runs 4 times
Attempts:
2 left
💡 Hint
Remember elseif ladder stops after first true condition.

Practice

(1/5)
1.

What does an elseif ladder do in PHP?

easy
A. It only checks the last condition in the ladder.
B. It runs all conditions regardless of their truth value.
C. It checks conditions one by one and runs the first true block.
D. It skips all conditions and runs the else block always.

Solution

  1. Step 1: Understand elseif ladder behavior

    An elseif ladder evaluates conditions from top to bottom.
  2. Step 2: Identify execution flow

    It executes the code block of the first condition that is true and skips the rest.
  3. Final Answer:

    It checks conditions one by one and runs the first true block. -> Option C
  4. Quick Check:

    Elseif ladder = first true condition runs [OK]
Hint: Runs only first true condition block in order [OK]
Common Mistakes:
  • Thinking all conditions run
  • Believing it checks only last condition
  • Assuming else always runs
2.

Which of the following is the correct syntax for an elseif ladder in PHP?

<?php
if (condition1) {
    // code
} elseif (condition2) {
    // code
} else {
    // code
}
?>
easy
A. if (condition1) {} elseif (condition2) {} else {}
B. if (condition1) {} else if (condition2) {} else {}
C. if (condition1) {} elseif condition2 {} else {}
D. if condition1 {} elseif (condition2) {} else {}

Solution

  1. Step 1: Check PHP elseif syntax

    PHP uses elseif (condition) with parentheses and no space between else and if.
  2. Step 2: Compare options

    if (condition1) {} elseif (condition2) {} else {} matches correct syntax; others have syntax errors like missing parentheses or wrong spacing.
  3. Final Answer:

    if (condition1) {} elseif (condition2) {} else {} -> Option A
  4. Quick Check:

    Correct elseif syntax = if (condition1) {} elseif (condition2) {} else {} [OK]
Hint: Use 'elseif' as one word with parentheses [OK]
Common Mistakes:
  • Writing 'else if' as two words in PHP
  • Omitting parentheses around conditions
  • Missing curly braces
3.

What will be the output of this PHP code?

<?php
$score = 75;
if ($score >= 90) {
    echo "Grade A";
} elseif ($score >= 80) {
    echo "Grade B";
} elseif ($score >= 70) {
    echo "Grade C";
} else {
    echo "Grade F";
}
?>
medium
A. Grade A
B. Grade F
C. Grade B
D. Grade C

Solution

  1. Step 1: Check conditions in order

    Score is 75. First condition (>=90) is false, second (>=80) false, third (>=70) true.
  2. Step 2: Determine executed block

    Since third condition is true, it prints "Grade C" and skips else.
  3. Final Answer:

    Grade C -> Option D
  4. Quick Check:

    75 >= 70 = Grade C [OK]
Hint: Check conditions top to bottom, first true runs [OK]
Common Mistakes:
  • Choosing Grade B or A incorrectly
  • Ignoring elseif order
  • Assuming else runs always
4.

Find the error in this PHP code snippet:

<?php
$age = 20;
if ($age > 18) {
    echo "Adult";
} elseif $age >= 13 {
    echo "Teenager";
} else {
    echo "Child";
}
?>
medium
A. Missing parentheses around elseif condition
B. Missing semicolon after echo statements
C. Incorrect variable name in elseif
D. No else block present

Solution

  1. Step 1: Review elseif syntax

    In PHP, conditions must be inside parentheses, but here elseif lacks parentheses.
  2. Step 2: Check other parts

    Echo statements have semicolons, variable names are correct, else block exists.
  3. Final Answer:

    Missing parentheses around elseif condition -> Option A
  4. Quick Check:

    Parentheses required for elseif condition [OK]
Hint: Always put conditions in parentheses [OK]
Common Mistakes:
  • Forgetting parentheses in elseif
  • Confusing semicolon placement
  • Assuming else block is optional
5.

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;
?>
hard
A. Syntax error due to missing elseif keyword
B. Correctly assigns categories as required
C. Assigns "Adult" for age 19 incorrectly
D. Assigns "Child" for age 13 incorrectly

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]
Hint: 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard