Nested loop execution in PHP - Time & Space Complexity
When we have loops inside loops, the time it takes to run the code can grow quickly.
We want to see how the work increases as the input gets bigger.
Analyze the time complexity of the following code snippet.
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $n; $j++) {
echo $i * $j . "\n";
}
}
This code prints the product of two numbers for every pair from 0 to n-1.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The inner loop's echo statement runs.
- How many times: The inner loop runs n times for each of the n outer loop runs, so n x n times.
As n grows, the total number of times the echo runs grows much faster.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 100 |
| 100 | 10,000 |
| 1000 | 1,000,000 |
Pattern observation: When n gets 10 times bigger, the work gets 100 times bigger.
Time Complexity: O(n2)
This means the work grows with the square of the input size, so doubling n makes the work about four times bigger.
[X] Wrong: "Since there are two loops, the time is just twice as long as one loop."
[OK] Correct: The loops are nested, so the inner loop runs completely for each outer loop step, multiplying the work, not just adding.
Understanding nested loops helps you explain how your code handles bigger inputs and shows you can think about efficiency clearly.
"What if the inner loop ran only up to a fixed number instead of n? How would the time complexity change?"
Practice
for ($i = 0; $i < 2; $i++) {
for ($j = 0; $j < 3; $j++) {
// code
}
}Solution
Step 1: Understand loop nesting order
The outer loop runs from 0 to 1 (2 times). For each outer loop iteration, the inner loop runs fully.Step 2: Inner loop completes all cycles per outer iteration
For each $i, $j runs from 0 to 2 (3 times), completing all inner cycles before next outer iteration.Final Answer:
The inner loop runs completely for each iteration of the outer loop. -> Option AQuick Check:
Inner loop finishes fully inside outer loop [OK]
- Thinking outer loop runs inside inner loop
- Assuming loops run only once
- Confusing simultaneous execution
for loop in PHP?Solution
Step 1: Check PHP for loop syntax
PHP requires parentheses around loop conditions and braces for blocks.Step 2: Validate nested loop syntax
for ($i = 0; $i < 2; $i++) { for ($j = 0; $j < 3; $j++) { echo $i . $j; } } uses correct parentheses and braces for both loops and echo statement inside.Final Answer:
for ($i = 0; $i < 2; $i++) { for ($j = 0; $j < 3; $j++) { echo $i . $j; } } -> Option BQuick Check:
Correct parentheses and braces in nested loops [OK]
- Omitting parentheses around loop conditions
- Missing braces for loop blocks
- Incorrect for loop syntax without $ sign
for ($i = 1; $i <= 2; $i++) {
for ($j = 1; $j <= 3; $j++) {
echo $i * $j . ' ';
}
}Solution
Step 1: Trace outer loop values
$i runs from 1 to 2.Step 2: Trace inner loop and calculate products
For $i=1: $j=1,2,3 -> 1*1=1, 1*2=2, 1*3=3
For $i=2: $j=1,2,3 -> 2*1=2, 2*2=4, 2*3=6Final Answer:
1 2 3 2 4 6 -> Option CQuick Check:
Multiply outer and inner loop counters [OK]
- Mixing up loop variable values
- Printing inner loop values only
- Ignoring multiplication in echo
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 2; $i++) {
echo $i . $j . ' ';
}
}Solution
Step 1: Check inner loop increment variable
The inner loop increments $i instead of $j, which is wrong.Step 2: Understand effect of wrong increment
Incrementing $i inside inner loop changes outer loop variable unexpectedly, causing infinite or wrong behavior.Final Answer:
Inner loop increments $i instead of $j causing infinite loop. -> Option AQuick Check:
Increment correct loop variable to avoid errors [OK]
- Incrementing wrong variable in inner loop
- Confusing loop conditions
- Assuming variables need declaration in PHP
?
Solution
Step 1: Understand output format
Each row should print products separated by spaces, then a newline after inner loop ends.Step 2: Check each option for correct newline placement
for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { echo $i * $j . ' '; } echo "\n"; } prints products with spaces inside inner loop, then newline after inner loop completes, correctly formatting rows.Final Answer:
for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { echo $i * $j . ' '; } echo "\n"; } -> Option DQuick Check:
Newline after inner loop completes for each row [OK]
- Printing newline inside inner loop
- Missing spaces between numbers
- No newline causing all output in one line
Ready to prove it?
Pass the test and this pattern moves unknown
