0
0
PHPprogramming~20 mins

If-else execution flow in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If-Else Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested if-else statements
What is the output of the following PHP code?
PHP
<?php
$value = 10;
if ($value > 5) {
    if ($value < 15) {
        echo "Inside range";
    } else {
        echo "Too high";
    }
} else {
    echo "Too low";
}
?>
AToo low
BInside range
CToo high
DNo output
Attempts:
2 left
💡 Hint
Check the conditions step by step starting from the outer if.
Predict Output
intermediate
2:00remaining
If-else with multiple conditions
What will this PHP code output?
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 C
BGrade B
CGrade A
DGrade F
Attempts:
2 left
💡 Hint
Check which condition matches the score starting from the top.
🔧 Debug
advanced
2:00remaining
Identify the error in if-else syntax
What error does this PHP code produce when run?
PHP
<?php
$number = 5;
if ($number > 0)
    echo "Positive";
else
    echo "Non-positive";
else
    echo "Zero";
?>
ANo output
BOutputs 'Positive'
COutputs 'Non-positive'
DSyntaxError: Unexpected 'else' after else
Attempts:
2 left
💡 Hint
Check the structure of if-else statements and how many else blocks are allowed.
🧠 Conceptual
advanced
2:00remaining
Understanding if-else execution flow
Consider this PHP code snippet:
$x = 3;
if ($x > 0) {
    echo "A";
} elseif ($x > 2) {
    echo "B";
} else {
    echo "C";
}

What will be the output?
AA
BB
CC
DNo output
Attempts:
2 left
💡 Hint
Remember that elseif is only checked if the previous if condition is false.
Predict Output
expert
2:00remaining
Output with complex if-else and logical operators
What is the output of this PHP code?
PHP
<?php
$a = 4;
$b = 7;
if ($a > 3 && $b < 10) {
    if ($a + $b > 10) {
        echo "X";
    } else {
        echo "Y";
    }
} else {
    echo "Z";
}
?>
ANo output
BZ
CX
DY
Attempts:
2 left
💡 Hint
Evaluate the conditions carefully, including the logical AND and sums.