For loop execution model in PHP - Time & Space Complexity
We want to understand how the time a for loop takes changes as we increase the number of times it runs.
How does the work inside the loop grow when the loop runs more times?
Analyze the time complexity of the following code snippet.
for ($i = 0; $i < $n; $i++) {
echo $i . "\n";
}
This code prints numbers from 0 up to one less than $n. It runs the loop $n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
echostatement inside the for loop. - How many times: Exactly
$ntimes, once for each loop cycle.
As $n grows, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times |
| 100 | 100 times |
| 1000 | 1000 times |
Pattern observation: The work grows directly with the input size. Double the input, double the work.
Time Complexity: O(n)
This means the time it takes grows in a straight line with the number of loop runs.
[X] Wrong: "The loop runs faster because it just prints numbers."
[OK] Correct: Printing still happens every time the loop runs, so the time grows with the number of loops, not faster.
Understanding how loops grow with input size helps you explain code efficiency clearly and confidently in interviews.
"What if we added a nested for loop inside this loop? How would the time complexity change?"
Practice
condition part in a PHP for loop?Solution
Step 1: Understand the for loop parts
A PHP for loop has three parts: initialization, condition, and increment.Step 2: Identify the condition's role
The condition is checked before each loop run to decide if the loop continues or stops.Final Answer:
It decides if the loop should continue running or stop. -> Option AQuick Check:
Condition controls loop continuation = It decides if the loop should continue running or stop. [OK]
- Confusing initialization with condition
- Thinking increment runs before loop starts
- Believing condition runs after loop ends
Solution
Step 1: Check for loop syntax rules
PHP for loops require parentheses around initialization, condition, and increment, separated by semicolons.Step 2: Validate each option
for ($i = 0; $i <= 4; $i++) {} uses correct syntax with $i starting at 0, condition $i <= 4, and increment $i++. Others have syntax errors or wrong increments.Final Answer:
for ($i = 0; $i <= 4; $i++) {} -> Option DQuick Check:
Correct for loop syntax = for ($i = 0; $i <= 4; $i++) {} [OK]
- Missing parentheses around for loop parts
- Using ++i without $ sign
- Wrong increment direction (i-- instead of i++)
$sum = 0;
for ($i = 1; $i <= 3; $i++) {
$sum += $i;
}
echo $sum;Solution
Step 1: Trace the loop iterations
The loop runs with $i = 1, 2, 3. Each time, $sum adds $i: 0+1=1, 1+2=3, 3+3=6.Step 2: Calculate final sum
After the loop, $sum is 6, which is printed by echo.Final Answer:
6 -> Option AQuick Check:
Sum 1+2+3 = 6 [OK]
- Concatenating numbers instead of adding
- Starting loop at 0 instead of 1
- Using wrong loop condition
for ($i = 0; $i < 5; $i) {
echo $i;
}Solution
Step 1: Examine the for loop header
The loop header has initialization ($i = 0), condition ($i < 5), but increment is just $i without ++ or --.Step 2: Identify the missing increment
Without increment like $i++, the loop variable never changes, causing an infinite loop or no progress.Final Answer:
Missing increment operation in the loop header. -> Option BQuick Check:
Increment missing = Missing increment operation in the loop header. [OK]
- Forgetting ++ or -- in increment
- Changing condition to wrong comparison
- Misplacing semicolons in loop header
Solution
Step 1: Understand the goal
We want to print even numbers from 2 to 10 inclusive.Step 2: Check each option
for ($i = 2; $i < 10; $i += 2) { echo $i . ' '; } prints 2 4 6 8, missing 10. for ($i = 1; $i <= 10; $i++) { if ($i % 2 == 0) echo $i . ' '; } loops 1 to 10 and prints only even numbers 2 4 6 8 10 correctly. for ($i = 2; $i < 10; $i++) { if ($i % 2 == 0) echo $i . ' '; } stops at 9, missing 10. for ($i = 0; $i <= 10; $i += 2) { echo $i . ' '; } starts at 0, which is not in the range 2 to 10.Step 3: Choose the best option
for ($i = 1; $i <= 10; $i++) { if ($i % 2 == 0) echo $i . ' '; } is correct and flexible, printing even numbers by checking inside the loop.Final Answer:
for ($i = 1; $i <= 10; $i++) { if ($i % 2 == 0) echo $i . ' '; } -> Option CQuick Check:
Loop 1-10, print if even = for ($i = 1; $i <= 10; $i++) { if ($i % 2 == 0) echo $i . ' '; } [OK]
- Stopping loop before 10
- Starting loop at 0 when not needed
- Forgetting to check even condition
Ready to prove it?
Pass the test and this pattern moves unknown
