Bird
Raised Fist0
PHPprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
If-Else Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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";
}
?>
AToo low
BInside range
CToo high
DNo output
Attempts:
2 left
💡 Hint
Check the conditions step by step starting from the outer if.
❓ Predict Output
intermediate
2: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";
}
?>
AGrade C
BGrade B
CGrade A
DGrade F
Attempts:
2 left
💡 Hint
Check which condition matches the score starting from the top.
🔧 Debug
advanced
2: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";
?>
ANo output
BOutputs 'Positive'
COutputs 'Non-positive'
DSyntaxError: Unexpected 'else' after else
Attempts:
2 left
💡 Hint
Check the structure of if-else statements and how many else blocks are allowed.
🧠 Conceptual
advanced
2:00remaining
Understanding if-else execution flow
Consider this PHP code snippet:
$x = 3;
if ($x > 0) {
    echo "A";
} elseif ($x > 2) {
    echo "B";
} else {
    echo "C";
}

What will be the output?
AA
BB
CC
DNo output
Attempts:
2 left
💡 Hint
Remember that elseif is only checked if the previous if condition is false.
❓ Predict Output
expert
2: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";
}
?>
ANo output
BZ
CX
DY
Attempts:
2 left
💡 Hint
Evaluate the conditions carefully, including the logical AND and sums.

Practice

(1/5)
1. What does an if-else statement do in PHP?
easy
A. It repeats code multiple times.
B. It chooses one block of code to run based on a condition.
C. It runs all blocks of code regardless of conditions.
D. It defines a function.

Solution

  1. Step 1: Understand the purpose of if-else

    An if-else statement checks a condition and runs code based on whether the condition is true or false.
  2. Step 2: Compare options with definition

    Only "It chooses one block of code to run based on a condition." correctly describes this behavior; others describe loops or functions.
  3. Final Answer:

    It chooses one block of code to run based on a condition. -> Option B
  4. Quick Check:

    If-else = choose one path [OK]
Hint: If-else picks one path based on true or false [OK]
Common Mistakes:
  • Thinking if-else runs all code blocks
  • Confusing if-else with loops
  • Believing if-else defines functions
2. Which of the following is the correct syntax for an if-else statement in PHP?
easy
A. if ($x > 10) { echo 'Yes' } else { echo 'No' }
B. if $x > 10 then echo 'Yes' else echo 'No'
C. if ($x > 10) echo 'Yes' else echo 'No'
D. if ($x > 10) { echo 'Yes'; } else { echo 'No'; }

Solution

  1. Step 1: Check PHP if-else syntax rules

    PHP requires parentheses around the condition and braces around the code blocks.
  2. Step 2: Identify correct syntax

    if ($x > 10) { echo 'Yes'; } else { echo 'No'; } uses parentheses and braces correctly. if $x > 10 then echo 'Yes' else echo 'No' uses invalid keywords. if ($x > 10) echo 'Yes' else echo 'No' misses braces which is allowed but needs semicolons. if ($x > 10) { echo 'Yes' } else { echo 'No' } misses semicolons inside braces.
  3. Final Answer:

    if ($x > 10) { echo 'Yes'; } else { echo 'No'; } -> Option D
  4. Quick Check:

    Correct syntax = if ($x > 10) { echo 'Yes'; } else { echo 'No'; } [OK]
Hint: Use parentheses and braces with semicolons in PHP if-else [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Missing semicolons after statements
  • Using invalid keywords like 'then'
3. What will be the output of this PHP code?
$score = 75;
if ($score >= 60) {
    echo "Pass";
} else {
    echo "Fail";
}
medium
A. Pass
B. Fail
C. 60
D. No output

Solution

  1. Step 1: Evaluate the condition $score >= 60

    The variable $score is 75, which is greater than or equal to 60, so the condition is true.
  2. Step 2: Determine which block runs

    Since the condition is true, the code inside the if block runs, printing "Pass".
  3. Final Answer:

    Pass -> Option A
  4. Quick Check:

    75 >= 60 is true, so output = Pass [OK]
Hint: Check if condition is true to pick if or else output [OK]
Common Mistakes:
  • Confusing true and false branches
  • Ignoring comparison operators
  • Expecting numeric output instead of string
4. Find the error in this PHP code snippet:
if ($age > 18)
    echo "Adult";
else
    echo "Minor"
medium
A. Missing parentheses around condition
B. Missing braces around if and else blocks
C. Missing semicolon after echo "Minor"
D. Incorrect comparison operator

Solution

  1. Step 1: Check syntax for echo statements

    Each echo statement must end with a semicolon in PHP.
  2. Step 2: Identify missing semicolon

    The echo "Minor" line is missing a semicolon at the end, causing a syntax error.
  3. Final Answer:

    Missing semicolon after echo "Minor" -> Option C
  4. Quick Check:

    Every statement needs semicolon [OK]
Hint: Always end echo statements with semicolon [OK]
Common Mistakes:
  • Forgetting semicolon after last statement
  • Thinking braces are always required
  • Misreading comparison operators
5. You want to write a PHP program that prints "Good morning" if the hour is less than 12, "Good afternoon" if the hour is between 12 and 17 (inclusive), and "Good evening" otherwise. Which code correctly implements this using if-else statements?
hard
A. if ($hour < 12) { echo "Good morning"; } else if ($hour <= 17) { echo "Good afternoon"; } else { echo "Good evening"; }
B. if ($hour < 12) { echo "Good morning"; } else if ($hour > 12 && $hour < 17) { echo "Good afternoon"; } else { echo "Good evening"; }
C. if ($hour <= 12) { echo "Good morning"; } else if ($hour >= 12 && $hour <= 17) { echo "Good afternoon"; } else { echo "Good evening"; }
D. if ($hour < 12) { echo "Good morning"; } else if ($hour >= 12 && $hour < 17) { echo "Good afternoon"; } else { echo "Good evening"; }

Solution

  1. Step 1: Analyze the conditions for each greeting

    "Good morning" if hour < 12, "Good afternoon" if hour between 12 and 17 inclusive, else "Good evening".
  2. Step 2: Check each option's condition ranges

    if ($hour < 12) { echo "Good morning"; } else if ($hour <= 17) { echo "Good afternoon"; } else { echo "Good evening"; } correctly uses $hour < 12, then $hour <= 17 for afternoon, covering 12 to 17 inclusive. Others either exclude 12 or 17 or overlap incorrectly.
  3. Final Answer:

    if ($hour < 12) { echo "Good morning"; } else if ($hour <= 17) { echo "Good afternoon"; } else { echo "Good evening"; } -> Option A
  4. Quick Check:

    Correct ranges cover all hours without gaps [OK]
Hint: Check boundary conditions carefully in if-else chains [OK]
Common Mistakes:
  • Using wrong comparison operators for boundaries
  • Overlapping or missing hour ranges
  • Confusing inclusive and exclusive conditions
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