Bird
0
0

What is the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Loops

What is the output of this PHP code?

$i = 0;
while ($i < 3) {
    $j = 0;
    while ($j < 3) {
        if ($j == 1) {
            break 2;
        }
        echo "$i$j ";
        $j++;
    }
    $i++;
}
A00 01 10 11 20 21
B00
C00 01 02
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze nested loops and break 2

    The inner loop runs with $j from 0 to 2. When $j == 1, break 2; exits both loops immediately.
  2. Step 2: Trace output before break

    Only when $j == 0, echo "$i$j "; prints "00 ". Then at $j == 1, break 2 stops all loops.
  3. Final Answer:

    00 -> Option B
  4. Quick Check:

    Break 2 exits both loops, output = 00 [OK]
Quick Trick: Break 2 exits two loops immediately [OK]
Common Mistakes:
  • Expecting all loop outputs before break
  • Confusing break with continue
  • Ignoring break level count

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes