Challenge - 5 Problems
Elseif Ladder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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"; } ?>
Attempts:
2 left
💡 Hint
Check which condition matches the score 75 first in the elseif ladder.
✗ Incorrect
The code checks conditions from top to bottom. Since 75 is not >= 90 or 80, but is >= 70, it prints 'Grade C'.
❓ Predict Output
intermediate2: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"; } ?>
Attempts:
2 left
💡 Hint
Check if 10 is greater than any of the conditions.
✗ Incorrect
10 is not greater than 30, 20, or 15, so the else block runs and prints 'Cold'.
🔧 Debug
advanced2: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"; } ?>
Attempts:
2 left
💡 Hint
Check the syntax of the elseif statement on line 5.
✗ Incorrect
The elseif statement is missing parentheses around the condition, causing a syntax error.
❓ Predict Output
advanced2: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"; } ?>
Attempts:
2 left
💡 Hint
Check which color matches the variable value.
✗ Incorrect
The variable $color is 'green', so the third elseif condition matches and prints 'Go'.
❓ Predict Output
expert2: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"; ?>
Attempts:
2 left
💡 Hint
Remember elseif ladder stops after first true condition.
✗ Incorrect
Only the first true condition's block runs (echo 'B'), plus the final echo outside the ladder, so echo runs twice total. count is 1.