Bird
Raised Fist0
PHPprogramming~5 mins

If statement execution flow in PHP - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does an if statement do in PHP?
An if statement checks a condition. If the condition is true, it runs the code inside its block. If false, it skips that code.
Click to reveal answer
beginner
What happens when the if condition is false and there is no else block?
The code inside the if block is skipped, and the program continues with the next statement after the if block.
Click to reveal answer
beginner
How does an else block work with an if statement?
The else block runs only when the if condition is false. It provides an alternative set of instructions.
Click to reveal answer
intermediate
What is the role of elseif in PHP's if statement execution flow?
elseif lets you check multiple conditions one after another. It runs the block for the first true condition it finds.
Click to reveal answer
intermediate
Explain the execution flow of this PHP code:
if ($a > 10) { echo 'Big'; } elseif ($a > 5) { echo 'Medium'; } else { echo 'Small'; }
PHP checks if $a is greater than 10. If yes, it prints 'Big'. If not, it checks if $a is greater than 5. If yes, it prints 'Medium'. Otherwise, it prints 'Small'. Only one block runs.
Click to reveal answer
What happens if the if condition is true?
AThe code inside the <code>if</code> block runs.
BThe code inside the <code>else</code> block runs.
CThe program stops.
DNothing happens.
Which block runs if the if condition is false and there is an else block?
A<code>else</code> block
BNo block runs
C<code>elseif</code> block
D<code>if</code> block
What does elseif allow you to do?
ARun code only if the first <code>if</code> is true
BRun code after the <code>else</code> block
CSkip all conditions
DCheck multiple conditions one after another
In this code, what will print if $a = 7?
if ($a > 10) { echo 'Big'; } elseif ($a > 5) { echo 'Medium'; } else { echo 'Small'; }
ABig
BMedium
CSmall
DNothing
If none of the if or elseif conditions are true, what happens?
AThe <code>if</code> block runs anyway
BThe program crashes
CThe <code>else</code> block runs if it exists
DNothing runs
Describe how PHP decides which block of code to run in an if-elseif-else statement.
Think about the order PHP checks conditions and what happens when one is true.
You got /5 concepts.
    Explain what happens when an if statement has no else block and the condition is false.
    Consider what PHP does when it finds no code to run inside the if.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does an if statement do in PHP?
      easy
      A. It defines a function.
      B. It repeats code multiple times.
      C. It runs code only if a condition is true.
      D. It stops the program immediately.

      Solution

      1. Step 1: Understand the purpose of an if statement

        An if statement checks a condition and runs code only if that condition is true.
      2. Step 2: Compare with other options

        Repeating code is done by loops, functions define reusable code, and stopping the program is done by exit or die.
      3. Final Answer:

        It runs code only if a condition is true. -> Option C
      4. Quick Check:

        If statement = runs code if true [OK]
      Hint: If checks condition; runs code only when true [OK]
      Common Mistakes:
      • Confusing if with loops
      • Thinking if defines functions
      • Believing if stops the program
      2. Which of the following is the correct syntax for an if statement in PHP?
      easy
      A. if condition { echo 'Yes'; }
      B. if (condition) { echo 'Yes'; }
      C. if condition: echo 'Yes'; endif;
      D. if [condition] { echo 'Yes'; }

      Solution

      1. Step 1: Recall PHP if statement syntax

        In PHP, the condition must be inside parentheses and the code block inside curly braces.
      2. Step 2: Check each option

        if (condition) { echo 'Yes'; } matches the correct syntax: if (condition) { ... }. if condition { echo 'Yes'; } misses parentheses, C uses alternate syntax but missing parentheses, D uses square brackets which is invalid.
      3. Final Answer:

        if (condition) { echo 'Yes'; } -> Option B
      4. Quick Check:

        Parentheses around condition = correct syntax [OK]
      Hint: Always use parentheses around condition in if [OK]
      Common Mistakes:
      • Omitting parentheses around condition
      • Using square brackets instead of parentheses
      • Confusing alternate syntax without proper format
      3. What will be the output of this PHP code?
      $x = 10;
      if ($x > 10) {
      echo 'Greater';
      } elseif ($x == 10) {
      echo 'Equal';
      } else {
      echo 'Less';
      }
      medium
      A. Equal
      B. No output
      C. Less
      D. Greater

      Solution

      1. Step 1: Evaluate the condition $x > 10

        $x is 10, so $x > 10 is false. The first if block is skipped.
      2. Step 2: Check the elseif condition $x == 10

        This condition is true, so it prints 'Equal' and skips the else block.
      3. Final Answer:

        Equal -> Option A
      4. Quick Check:

        10 == 10 prints Equal [OK]
      Hint: Check conditions top to bottom; first true runs [OK]
      Common Mistakes:
      • Assuming > includes equal
      • Ignoring elseif and jumping to else
      • Thinking multiple blocks run
      4. Find the error in this PHP code:
      if $a > 5 {
      echo 'Big';
      } else {
      echo 'Small';
      }
      medium
      A. Wrong keyword else
      B. Missing semicolon after echo
      C. Curly braces not allowed
      D. Missing parentheses around condition

      Solution

      1. Step 1: Check if statement syntax

        In PHP, the condition must be inside parentheses. Here, if $a > 5 is missing parentheses.
      2. Step 2: Verify other parts

        Semicolons after echo statements are optional if echo is used as a statement, else keyword is correct, and curly braces are allowed.
      3. Final Answer:

        Missing parentheses around condition -> Option D
      4. Quick Check:

        Condition must be in parentheses [OK]
      Hint: Always put condition inside parentheses in if [OK]
      Common Mistakes:
      • Forgetting parentheses around condition
      • Thinking else keyword is wrong
      • Believing semicolon is mandatory after echo
      5. Given this PHP code, what will be the output?
      $score = 75;
      if ($score >= 90) {
      echo 'A';
      } elseif ($score >= 80) {
      echo 'B';
      } elseif ($score >= 70) {
      echo 'C';
      } else {
      echo 'F';
      }
      hard
      A. C
      B. A
      C. B
      D. F

      Solution

      1. Step 1: Check first condition $score >= 90

        75 is less than 90, so this is false. Skip to next.
      2. Step 2: Check second condition $score >= 80

        75 is less than 80, so false again. Skip to next.
      3. Step 3: Check third condition $score >= 70

        75 is greater than or equal to 70, so true. It prints 'C' and skips else.
      4. Final Answer:

        C -> Option A
      5. Quick Check:

        75 >= 70 prints C [OK]
      Hint: Check conditions top-down; first true runs [OK]
      Common Mistakes:
      • Choosing wrong grade for score
      • Ignoring elseif order
      • Assuming else runs always
      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