If statement execution flow in PHP - Time & Space Complexity
We want to see how the time it takes to run an if statement changes as the input changes.
Specifically, does adding more data make the if statement take longer?
Analyze the time complexity of the following code snippet.
$number = 10;
if ($number > 0) {
echo "Positive number";
} else {
echo "Zero or negative number";
}
This code checks if a number is positive and prints a message based on that.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single if condition check
- How many times: Exactly once per run
The if statement runs once no matter the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of operations stays the same even if input grows.
Time Complexity: O(1)
This means the time to run the if statement does not change with input size.
[X] Wrong: "If statements take longer when input is bigger."
[OK] Correct: The if condition runs once regardless of input size, so time stays the same.
Understanding that simple decisions like if statements run in constant time helps you explain code efficiency clearly and confidently.
"What if the if statement was inside a loop that runs n times? How would the time complexity change?"
Practice
if statement do in PHP?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 CQuick Check:
If statement = runs code if true [OK]
- Confusing if with loops
- Thinking if defines functions
- Believing if stops the program
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 BQuick Check:
Parentheses around condition = correct syntax [OK]
- Omitting parentheses around condition
- Using square brackets instead of parentheses
- Confusing alternate syntax without proper format
$x = 10;
if ($x > 10) {
echo 'Greater';
} elseif ($x == 10) {
echo 'Equal';
} else {
echo 'Less';
}
Solution
Step 1: Evaluate the condition $x > 10
$x is 10, so $x > 10 is false. The first if block is skipped.Step 2: Check the elseif condition $x == 10
This condition is true, so it prints 'Equal' and skips the else block.Final Answer:
Equal -> Option AQuick Check:
10 == 10 prints Equal [OK]
- Assuming > includes equal
- Ignoring elseif and jumping to else
- Thinking multiple blocks run
if $a > 5 {
echo 'Big';
} else {
echo 'Small';
}Solution
Step 1: Check if statement syntax
In PHP, the condition must be inside parentheses. Here,if $a > 5is missing parentheses.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.Final Answer:
Missing parentheses around condition -> Option DQuick Check:
Condition must be in parentheses [OK]
- Forgetting parentheses around condition
- Thinking else keyword is wrong
- Believing semicolon is mandatory after echo
$score = 75;
if ($score >= 90) {
echo 'A';
} elseif ($score >= 80) {
echo 'B';
} elseif ($score >= 70) {
echo 'C';
} else {
echo 'F';
}
Solution
Step 1: Check first condition $score >= 90
75 is less than 90, so this is false. Skip to next.Step 2: Check second condition $score >= 80
75 is less than 80, so false again. Skip to next.Step 3: Check third condition $score >= 70
75 is greater than or equal to 70, so true. It prints 'C' and skips else.Final Answer:
C -> Option AQuick Check:
75 >= 70 prints C [OK]
- Choosing wrong grade for score
- Ignoring elseif order
- Assuming else runs always
Ready to prove it?
Pass the test and this pattern moves unknown
