0
0
PHPprogramming~10 mins

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

Choose your learning style9 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?
YesRepeat Inner Loop
No
Update i
Check i < limit?
YesRepeat 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.