0
0
PHPprogramming~20 mins

Nested conditional execution in PHP - Practice Problems & Coding Challenges

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