Bird
0
0

What happens when a nested loop runs in PHP?

easy📝 Conceptual Q11 of 15
PHP - Loops
What happens when a nested loop runs in PHP?
for ($i = 0; $i < 2; $i++) {
for ($j = 0; $j < 3; $j++) {
// code
}
}
AThe inner loop runs completely for each iteration of the outer loop.
BThe outer loop runs completely for each iteration of the inner loop.
CBoth loops run only once.
DThe loops run simultaneously without nesting.
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes