0
0
PHPprogramming~20 mins

Elseif ladder execution in PHP - Practice Problems & Coding Challenges

Choose your learning style9 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.