0
0
PHPprogramming~5 mins

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

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