Bird
Raised Fist0
PHPprogramming~20 mins

Nested conditional execution in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Condition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested if-else blocks
What is the output of this PHP code snippet?
PHP
<?php
$score = 75;
if ($score >= 90) {
    echo "Grade: A";
} else {
    if ($score >= 70) {
        echo "Grade: B";
    } else {
        echo "Grade: C";
    }
}
?>
ANo output
BGrade: A
CGrade: C
DGrade: B
Attempts:
2 left
💡 Hint
Check the first condition and then the nested condition inside the else block.
❓ Predict Output
intermediate
2:00remaining
Nested condition with logical operators
What will this PHP code output?
PHP
<?php
$age = 20;
$hasID = true;
if ($age >= 18) {
    if ($hasID) {
        echo "Access granted";
    } else {
        echo "ID required";
    }
} else {
    echo "Access denied";
}
?>
ANo output
BID required
CAccess granted
DAccess denied
Attempts:
2 left
💡 Hint
Check both conditions inside the nested if.
🔧 Debug
advanced
2:00remaining
Identify the error in nested condition
What error does this PHP code produce when run?
PHP
<?php
$number = 10;
if ($number > 0) {
    if $number < 20 {
        echo "Number is between 1 and 19";
    }
}
?>
AParse error: syntax error, unexpected '$number' (T_VARIABLE)
BNo output
COutput: Number is between 1 and 19
DFatal error: Uncaught Error: Undefined variable
Attempts:
2 left
💡 Hint
Check the syntax of the nested if statement.
❓ Predict Output
advanced
2:00remaining
Output of nested if-elseif-else
What is the output of this PHP code?
PHP
<?php
$val = 15;
if ($val > 20) {
    echo "Greater than 20";
} elseif ($val > 10) {
    if ($val % 2 == 0) {
        echo "Even and greater than 10";
    } else {
        echo "Odd and greater than 10";
    }
} else {
    echo "10 or less";
}
?>
AOdd and greater than 10
BEven and greater than 10
CGreater than 20
D10 or less
Attempts:
2 left
💡 Hint
Check the elseif condition and then the nested if inside it.
🧠 Conceptual
expert
3:00remaining
Understanding nested conditional flow
Consider this PHP code snippet. What will be the value of $result after execution?
PHP
<?php
$a = 5;
$b = 10;
if ($a > $b) {
    if ($a - $b > 3) {
        $result = 'A';
    } else {
        $result = 'B';
    }
} else {
    if ($b - $a > 3) {
        $result = 'C';
    } else {
        $result = 'D';
    }
}
?>
A"B"
B"C"
C"A"
D"D"
Attempts:
2 left
💡 Hint
Compare values of $a and $b and check the nested conditions carefully.

Practice

(1/5)
1.

What does nested conditional execution mean in PHP?

easy
A. Running PHP code without any conditions
B. Writing multiple if statements one after another without nesting
C. Using only one if statement without any else
D. Using an if or else statement inside another if or else block

Solution

  1. Step 1: Understand conditional statements

    Conditional statements like if and else let the program choose actions based on conditions.
  2. Step 2: Define nested conditionals

    Nested conditionals mean placing one conditional inside another to check multiple conditions step-by-step.
  3. Final Answer:

    Using an if or else statement inside another if or else block -> Option D
  4. Quick Check:

    Nested conditionals = condition inside condition [OK]
Hint: Look for conditions placed inside other conditions [OK]
Common Mistakes:
  • Thinking multiple separate ifs are nested
  • Confusing nested with chained conditions
  • Ignoring the inner condition placement
2.

Which of the following is the correct syntax for nested if statements in PHP?

<?php
if ($a > 0) {
    if ($b < 10) {
        echo "Inside nested if";
    }
}
?>
easy
A. Using elseif instead of nested if
B. Correct syntax with braces and proper nesting
C. Missing braces around inner if block
D. Using parentheses incorrectly around conditions

Solution

  1. Step 1: Check braces usage

    Both outer and inner if statements have braces to define their blocks properly.
  2. Step 2: Verify condition syntax

    Conditions use parentheses correctly and logical operators are valid.
  3. Final Answer:

    Correct syntax with braces and proper nesting -> Option B
  4. Quick Check:

    Proper braces and nesting = correct syntax [OK]
Hint: Always use braces {} for nested blocks [OK]
Common Mistakes:
  • Omitting braces causing syntax errors
  • Misplacing parentheses in conditions
  • Confusing elseif with nested if
3.

What will be the output of the following PHP code?

<?php
$x = 5;
$y = 8;
if ($x > 3) {
    if ($y < 10) {
        echo "A";
    } else {
        echo "B";
    }
} else {
    echo "C";
}
?>
medium
A. No output
B. B
C. A
D. C

Solution

  1. Step 1: Evaluate outer if condition

    $x = 5, which is greater than 3, so the outer if block runs.
  2. Step 2: Evaluate inner if condition

    $y = 8, which is less than 10, so the inner if block runs and prints "A".
  3. Final Answer:

    A -> Option C
  4. Quick Check:

    5 > 3 and 8 < 10 = A [OK]
Hint: Check outer condition first, then inner [OK]
Common Mistakes:
  • Ignoring inner condition result
  • Confusing else blocks
  • Assuming else runs when condition is true
4.

Find the error in this nested conditional PHP code:

<?php
$a = 10;
if ($a > 5)
    if ($a < 15)
        echo "Inside nested if";
    else
        echo "Outside nested if";
?>
medium
A. Missing braces causing else to pair with wrong if
B. Incorrect comparison operators
C. Echo statements missing semicolons
D. Variable $a not defined

Solution

  1. Step 1: Analyze if-else pairing without braces

    Without braces, the else pairs with the nearest if, which is the inner one.
  2. Step 2: Understand the problem

    This can cause confusion or unexpected behavior because the outer if has no else block explicitly.
  3. Final Answer:

    Missing braces causing else to pair with wrong if -> Option A
  4. Quick Check:

    Braces needed to clarify else pairing [OK]
Hint: Always use braces to avoid else ambiguity [OK]
Common Mistakes:
  • Assuming else pairs with outer if
  • Ignoring indentation as syntax guide
  • Missing semicolons (not the issue here)
5.

Given the following PHP code, what will be the output?

<?php
$score = 75;
if ($score >= 90) {
    echo "Grade A";
} else {
    if ($score >= 75) {
        if ($score >= 80) {
            echo "Grade B+";
        } else {
            echo "Grade B";
        }
    } else {
        echo "Grade C";
    }
}
?>
hard
A. Grade B
B. Grade B+
C. Grade C
D. Grade A

Solution

  1. Step 1: Check first condition

    $score = 75, which is not >= 90, so skip first if block.
  2. Step 2: Check second condition

    $score >= 75 is true, so enter second if.
  3. Step 3: Check nested condition inside second if

    $score >= 80 is false (75 < 80), so execute else and print "Grade B".
  4. Final Answer:

    Grade B -> Option A
  5. Quick Check:

    75 >= 75 but < 80 = Grade B [OK]
Hint: Follow conditions top-down carefully [OK]
Common Mistakes:
  • Confusing >= 75 and >= 80 checks
  • Skipping nested else block
  • Assuming 75 is >= 80
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard