If-else execution flow in PHP - Time & Space Complexity
We want to understand how the time it takes to run if-else statements changes as the input changes.
Specifically, we ask: does adding more conditions or inputs make the program slower?
Analyze the time complexity of the following code snippet.
function checkNumber($num) {
if ($num > 0) {
return "Positive";
} elseif ($num < 0) {
return "Negative";
} else {
return "Zero";
}
}
This code checks if a number is positive, negative, or zero and returns a string accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single if-else checks (no loops or recursion)
- How many times: Each condition is checked once per function call
The time to run this code stays about the same no matter what number you give it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 checks |
| 100 | 3 checks |
| 1000 | 3 checks |
Pattern observation: The number of checks does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the time to run the code stays the same no matter how big the input number is.
[X] Wrong: "If I give a bigger number, the code will take longer because it has to check more things."
[OK] Correct: The code only checks fixed conditions once, so bigger numbers don't add more checks.
Understanding that simple if-else checks run in constant time helps you explain how decisions in code affect speed.
"What if we added a loop that runs the if-else check multiple times? How would the time complexity change?"
Practice
if-else statement do in PHP?Solution
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.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.Final Answer:
It chooses one block of code to run based on a condition. -> Option BQuick Check:
If-else = choose one path [OK]
- Thinking if-else runs all code blocks
- Confusing if-else with loops
- Believing if-else defines functions
Solution
Step 1: Check PHP if-else syntax rules
PHP requires parentheses around the condition and braces around the code blocks.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.Final Answer:
if ($x > 10) { echo 'Yes'; } else { echo 'No'; } -> Option DQuick Check:
Correct syntax = if ($x > 10) { echo 'Yes'; } else { echo 'No'; } [OK]
- Omitting parentheses around condition
- Missing semicolons after statements
- Using invalid keywords like 'then'
$score = 75;
if ($score >= 60) {
echo "Pass";
} else {
echo "Fail";
}Solution
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.Step 2: Determine which block runs
Since the condition is true, the code inside the if block runs, printing "Pass".Final Answer:
Pass -> Option AQuick Check:
75 >= 60 is true, so output = Pass [OK]
- Confusing true and false branches
- Ignoring comparison operators
- Expecting numeric output instead of string
if ($age > 18)
echo "Adult";
else
echo "Minor"Solution
Step 1: Check syntax for echo statements
Each echo statement must end with a semicolon in PHP.Step 2: Identify missing semicolon
The echo "Minor" line is missing a semicolon at the end, causing a syntax error.Final Answer:
Missing semicolon after echo "Minor" -> Option CQuick Check:
Every statement needs semicolon [OK]
- Forgetting semicolon after last statement
- Thinking braces are always required
- Misreading comparison operators
Solution
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".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.Final Answer:
if ($hour < 12) { echo "Good morning"; } else if ($hour <= 17) { echo "Good afternoon"; } else { echo "Good evening"; } -> Option AQuick Check:
Correct ranges cover all hours without gaps [OK]
- Using wrong comparison operators for boundaries
- Overlapping or missing hour ranges
- Confusing inclusive and exclusive conditions
Ready to prove it?
Pass the test and this pattern moves unknown
