Bird
0
0

Find the error in this PHP code snippet:

medium📝 Debug Q14 of 15
PHP - Loops

Find the error in this PHP code snippet:

for ($i = 0; $i < 3; $i++) {
    for ($j = 0; $j < 3; $j++) {
        if ($i == $j) {
            break 3;
        }
        echo "$i,$j\n";
    }
}
Abreak 3; is invalid because only 2 loops exist.
Bbreak 3; should be break 2; to exit both loops.
Cbreak 3; causes infinite loop.
DNo error, code runs fine.
Step-by-Step Solution
Solution:
  1. Step 1: Count nested loops

    There are 2 nested loops: outer and inner.
  2. Step 2: Check break level usage

    Using break 3; tries to break out of 3 loops, but only 2 exist, causing a fatal error.
  3. Final Answer:

    break 3; is invalid because only 2 loops exist. -> Option A
  4. Quick Check:

    break n must be ≤ nested loops count [OK]
Quick Trick: Break level cannot exceed nested loops count [OK]
Common Mistakes:
  • Using break level higher than loops
  • Assuming break 3 exits 2 loops
  • Ignoring PHP fatal error on invalid break level

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes