Bird
Raised Fist0
PHPprogramming~10 mins

Nested loop execution in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Nested loop execution
Start Outer Loop i=0
↓
Start Inner Loop j=0
↓
Execute Inner Loop Body
↓
Update j
↓
Check j < limit?
Yes→Repeat Inner Loop
No↓
Update i
↓
Check i < limit?
Yes→Repeat Outer Loop
No↓
End
The outer loop starts and for each iteration, the inner loop runs completely before the outer loop moves to the next iteration.
Execution Sample
PHP
<?php
for ($i = 0; $i < 2; $i++) {
    for ($j = 0; $j < 3; $j++) {
        echo "$i,$j\n";
    }
}
?>
This code runs two loops: the outer loop runs 2 times, and for each outer loop, the inner loop runs 3 times, printing pairs of i and j.
Execution Table
StepijCondition i<2Condition j<3ActionOutput
100TrueTruePrint '0,0'0,0
201TrueTruePrint '0,1'0,1
302TrueTruePrint '0,2'0,2
403TrueFalseInner loop ends, increment i
510TrueTruePrint '1,0'1,0
611TrueTruePrint '1,1'1,1
712TrueTruePrint '1,2'1,2
813TrueFalseInner loop ends, increment i
920FalseN/AOuter loop ends
💡 Outer loop i=2 is not less than 2, so the loop stops.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8Final
i0000011112
j001230123N/A
Key Moments - 3 Insights
Why does the inner loop run completely before the outer loop increments?
Because in the execution_table rows 1-4 and 5-8, you can see j goes from 0 to 3 fully for each i before i increments.
Why does the outer loop stop when i equals 2?
Row 9 shows condition i<2 is False, so the outer loop ends as per the exit_note.
What happens to variable j after the inner loop ends?
At steps 4 and 8, j reaches 3 which fails the inner loop condition, then resets to 0 when the inner loop starts again for the next i.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of j at Step 3?
A3
B2
C1
D0
💡 Hint
Check the 'j' column in execution_table row 3.
At which step does the outer loop condition become false?
AStep 9
BStep 4
CStep 8
DStep 1
💡 Hint
Look at the 'Condition i<2' column and exit_note in execution_table.
If the inner loop condition changed to j<2, how many times would 'Print' action happen in total?
A8
B6
C4
D3
💡 Hint
Multiply outer loop count (2) by new inner loop count (2) for total prints.
Concept Snapshot
Nested loops run one inside another.
Outer loop runs first.
Inner loop runs fully each outer iteration.
Inner loop resets each time outer loop increments.
Loops stop when their conditions fail.
Full Transcript
This example shows nested loops in PHP. The outer loop variable i starts at 0 and runs while less than 2. For each i, the inner loop variable j runs from 0 to less than 3. Each pair i,j is printed. The inner loop completes fully before the outer loop increments. When i reaches 2, the outer loop stops. Variables i and j change as shown in the variable tracker. Key points include the inner loop running fully each outer iteration and loops stopping when conditions fail.

Practice

(1/5)
1. What happens when a nested loop runs in PHP?
for ($i = 0; $i < 2; $i++) {
for ($j = 0; $j < 3; $j++) {
// code
}
}
easy
A. The inner loop runs completely for each iteration of the outer loop.
B. The outer loop runs completely for each iteration of the inner loop.
C. Both loops run only once.
D. The loops run simultaneously without nesting.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    The inner loop runs completely for each iteration of the outer loop. -> Option A
  4. Quick Check:

    Inner loop finishes fully inside outer loop [OK]
Hint: Inner loop runs fully inside each outer loop cycle [OK]
Common Mistakes:
  • Thinking outer loop runs inside inner loop
  • Assuming loops run only once
  • Confusing simultaneous execution
2. Which of the following is the correct syntax for a nested for loop in PHP?
easy
A. for ($i = 0; $i < 2; $i++) { for $j = 0; $j < 3; $j++ { echo $i . $j; } }
B. for ($i = 0; $i < 2; $i++) { for ($j = 0; $j < 3; $j++) { echo $i . $j; } }
C. for ($i = 0; $i < 2; $i++) for ($j = 0; $j < 3; $j++) echo $i . $j;
D. for $i = 0; $i < 2; $i++ { for $j = 0; $j < 3; $j++ { echo $i . $j; } }

Solution

  1. Step 1: Check PHP for loop syntax

    PHP requires parentheses around loop conditions and braces for blocks.
  2. 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.
  3. Final Answer:

    for ($i = 0; $i < 2; $i++) { for ($j = 0; $j < 3; $j++) { echo $i . $j; } } -> Option B
  4. Quick Check:

    Correct parentheses and braces in nested loops [OK]
Hint: Use parentheses and braces properly in PHP loops [OK]
Common Mistakes:
  • Omitting parentheses around loop conditions
  • Missing braces for loop blocks
  • Incorrect for loop syntax without $ sign
3. What is the output of this PHP code?
for ($i = 1; $i <= 2; $i++) {
for ($j = 1; $j <= 3; $j++) {
echo $i * $j . ' ';
}
}
medium
A. 1 2 3 1 2 3
B. 1 4 9 2 5 8
C. 1 2 3 2 4 6
D. 6 12 18 6 12 18

Solution

  1. Step 1: Trace outer loop values

    $i runs from 1 to 2.
  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=6
  3. Final Answer:

    1 2 3 2 4 6 -> Option C
  4. Quick Check:

    Multiply outer and inner loop counters [OK]
Hint: Multiply outer and inner counters for output [OK]
Common Mistakes:
  • Mixing up loop variable values
  • Printing inner loop values only
  • Ignoring multiplication in echo
4. Find the error in this nested loop PHP code:
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 2; $i++) {
echo $i . $j . ' ';
}
}
medium
A. Inner loop increments $i instead of $j causing infinite loop.
B. Outer loop condition is incorrect.
C. Missing semicolon after echo statement.
D. Variables $i and $j are not declared.

Solution

  1. Step 1: Check inner loop increment variable

    The inner loop increments $i instead of $j, which is wrong.
  2. Step 2: Understand effect of wrong increment

    Incrementing $i inside inner loop changes outer loop variable unexpectedly, causing infinite or wrong behavior.
  3. Final Answer:

    Inner loop increments $i instead of $j causing infinite loop. -> Option A
  4. Quick Check:

    Increment correct loop variable to avoid errors [OK]
Hint: Increment inner loop variable inside inner loop [OK]
Common Mistakes:
  • Incrementing wrong variable in inner loop
  • Confusing loop conditions
  • Assuming variables need declaration in PHP
5. You want to print a multiplication table from 1 to 3 using nested loops in PHP. Which code correctly prints the table with each row on a new line?
?
hard
A. for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { echo $i * $j; } echo "\n"; }
B. for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { echo $i * $j . "\n"; } }
C. for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { echo $i * $j . ' '; } }
D. for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { echo $i * $j . ' '; } echo "\n"; }

Solution

  1. Step 1: Understand output format

    Each row should print products separated by spaces, then a newline after inner loop ends.
  2. 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.
  3. Final Answer:

    for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { echo $i * $j . ' '; } echo "\n"; } -> Option D
  4. Quick Check:

    Newline after inner loop completes for each row [OK]
Hint: Print newline after inner loop to separate rows [OK]
Common Mistakes:
  • Printing newline inside inner loop
  • Missing spaces between numbers
  • No newline causing all output in one line
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard