0
0
PHPprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If Statement Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP if-else code?

Look at the PHP code below. What will it print?

PHP
<?php
$number = 10;
if ($number > 10) {
    echo "Greater than 10";
} elseif ($number == 10) {
    echo "Equal to 10";
} else {
    echo "Less than 10";
}
?>
AEqual to 10
BGreater than 10
CLess than 10
DNo output
Attempts:
2 left
💡 Hint

Check the condition that matches the value 10 exactly.

Predict Output
intermediate
2:00remaining
What will this PHP code output?

Consider this PHP snippet. What is printed?

PHP
<?php
$value = 0;
if ($value) {
    echo "True";
} else {
    echo "False";
}
?>
ATrue
B0
CFalse
DNo output
Attempts:
2 left
💡 Hint

Remember how PHP treats 0 in conditions.

🔧 Debug
advanced
2:00remaining
Why does this PHP if statement not print anything?

Look at this PHP code. It prints nothing. Why?

PHP
<?php
$score = 75;
if ($score > 80);
{
    echo "Passed";
}
?>
AThe semicolon after the if condition ends the if statement early, so the echo always runs.
BThe variable $score is not defined correctly.
CThe condition is false, so echo is skipped.
DPHP does not allow if statements without braces.
Attempts:
2 left
💡 Hint

Check the semicolon after the if condition.

Predict Output
advanced
2:00remaining
What is the output of nested if statements in PHP?

What does this PHP code print?

PHP
<?php
$age = 20;
if ($age > 18) {
    if ($age < 21) {
        echo "Young adult";
    } else {
        echo "Adult";
    }
} else {
    echo "Minor";
}
?>
ANo output
BAdult
CMinor
DYoung adult
Attempts:
2 left
💡 Hint

Check both conditions carefully.

🧠 Conceptual
expert
2:00remaining
How many times does this PHP if-elseif-else chain execute echo?

Consider this PHP code. How many times will it print something?

PHP
<?php
$val = 5;
if ($val > 3) {
    echo "A";
} elseif ($val > 4) {
    echo "B";
} else {
    echo "C";
}
?>
A2
B1
C0
D3
Attempts:
2 left
💡 Hint

Remember that only one block in if-elseif-else runs.