Bird
0
0

Consider this PHP code:

hard📝 Application Q9 of 15
PHP - Conditional Statements
Consider this PHP code:
$x = 5;
if ($x > 0) {
    if ($x % 2 == 0) {
        echo "Even";
    } else {
        echo "Odd";
    }
} else {
    echo "Non-positive";
}

What will be the output?
AError
BOdd
CNon-positive
DEven
Step-by-Step Solution
Solution:
  1. Step 1: Check outer if condition $x > 0

    Since $x is 5, which is greater than 0, the outer if block runs.
  2. Step 2: Check inner if condition $x % 2 == 0

    5 modulo 2 is 1, so condition is false; inner else runs printing "Odd".
  3. Final Answer:

    Odd -> Option B
  4. Quick Check:

    Nested if else correctly identifies odd number [OK]
Quick Trick: Nested if checks conditions stepwise [OK]
Common Mistakes:
  • Confusing modulo result
  • Assuming 5 is even
  • Thinking else runs for outer if

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes