Challenge - 5 Problems
If-Else Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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"; } ?>
Attempts:
2 left
💡 Hint
Check the conditions step by step starting from the outer if.
✗ Incorrect
The variable $value is 10, which is greater than 5 and less than 15, so the inner if condition is true and it prints 'Inside range'.
❓ Predict Output
intermediate2: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"; } ?>
Attempts:
2 left
💡 Hint
Check which condition matches the score starting from the top.
✗ Incorrect
The score is 75, which is not >= 90 or >= 80, but is >= 70, so it prints 'Grade C'.
🔧 Debug
advanced2: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"; ?>
Attempts:
2 left
💡 Hint
Check the structure of if-else statements and how many else blocks are allowed.
✗ Incorrect
The code has two else blocks for one if, which is invalid syntax in PHP and causes a syntax error.
🧠 Conceptual
advanced2:00remaining
Understanding if-else execution flow
Consider this PHP code snippet:
What will be the output?
$x = 3;
if ($x > 0) {
echo "A";
} elseif ($x > 2) {
echo "B";
} else {
echo "C";
}What will be the output?
Attempts:
2 left
💡 Hint
Remember that elseif is only checked if the previous if condition is false.
✗ Incorrect
Since $x is 3, the first if condition ($x > 0) is true, so it prints 'A' and skips the elseif and else blocks.
❓ Predict Output
expert2: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"; } ?>
Attempts:
2 left
💡 Hint
Evaluate the conditions carefully, including the logical AND and sums.
✗ Incorrect
Both conditions in the outer if are true ($a=4 >3 and $b=7 <10). The inner if checks if 4+7>10, which is true, so it prints 'X'.