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.
✗ Incorrect
When the if condition is true, PHP runs the code inside the if block.
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
✗ Incorrect
If the if condition is false, the else block runs if it exists.
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
✗ Incorrect
elseif lets you check more than one condition in order.
7 is not greater than 10, but it is greater than 5, so 'Medium' prints.
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
✗ Incorrect
The else block runs when all previous conditions are false.
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
Step 1: Understand the purpose of an if statement
An if statement checks a condition and runs code only if that condition is true.
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.
Final Answer:
It runs code only if a condition is true. -> Option C
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
Step 1: Recall PHP if statement syntax
In PHP, the condition must be inside parentheses and the code block inside curly braces.
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.
Final Answer:
if (condition) { echo 'Yes'; } -> Option B
Quick Check:
Parentheses around condition = correct syntax [OK]
Hint: Always use parentheses around condition in if [OK]