Bird
Raised Fist0
PHPprogramming~20 mins

Break statement with levels in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Break Mastery in PHP
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested loops with break 2
What is the output of this PHP code using break 2 inside nested loops?
PHP
<?php
for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        if ($j == 2) {
            break 2;
        }
        echo "$i$j ";
    }
}
?>
A11
B11 12 13 21 22 23 31 32 33
C11 12
D12
Attempts:
2 left
💡 Hint
Remember that break 2 exits two levels of loops immediately.
❓ Predict Output
intermediate
2:00remaining
Effect of break 1 in nested loops
What will be the output of this PHP code using break 1 inside nested loops?
PHP
<?php
for ($i = 1; $i <= 2; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        if ($j == 2) {
            break 1;
        }
        echo "$i$j ";
    }
}
?>
A11 12 21 22
B11 21
C11 12 13 21 22 23
D11 21 22
Attempts:
2 left
💡 Hint
break 1 only exits the innermost loop.
🔧 Debug
advanced
2:00remaining
Identify error with break level
What error will this PHP code produce when using break 3 inside two nested loops?
PHP
<?php
for ($i = 0; $i < 2; $i++) {
    for ($j = 0; $j < 2; $j++) {
        if ($j == 1) {
            break 3;
        }
        echo "$i$j ";
    }
}
?>
AFatal error: Cannot break 3 levels
BNo output, loops run fully
COutput: 0001
DSyntax error: unexpected break
Attempts:
2 left
💡 Hint
Check how many nested loops exist and the break level used.
❓ Predict Output
advanced
2:00remaining
Output with break in switch inside loops
What is the output of this PHP code with break 2 inside a switch within nested loops?
PHP
<?php
for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        switch ($j) {
            case 2:
                break 2;
            default:
                echo "$i$j ";
        }
    }
}
?>
A11 13 21 23 31 33
B11 12 13 21 22 23 31 32 33
C11
D12
Attempts:
2 left
💡 Hint
Remember break 2 exits two control structures, here loops and switch.
🧠 Conceptual
expert
3:00remaining
Understanding break levels in complex nested structures
Consider this PHP code with three nested loops and a break 2 inside the innermost loop. After the break executes, which loops continue running?
PHP
<?php
for ($a = 1; $a <= 2; $a++) {
    for ($b = 1; $b <= 2; $b++) {
        for ($c = 1; $c <= 2; $c++) {
            if ($c == 2) {
                break 2;
            }
            echo "$a$b$c ";
        }
    }
}
?>
AOnly the outermost loop continues; the two inner loops break
BOnly the innermost loop breaks; middle and outer loops continue
CAll three loops break and stop running
DThe innermost and middle loops break; outer loop continues
Attempts:
2 left
💡 Hint
Break level counts how many nested loops to exit starting from innermost.

Practice

(1/5)
1.

What does the break statement do in PHP loops?

easy
A. It skips the current iteration and continues the loop.
B. It pauses the loop until a condition is met.
C. It restarts the loop from the beginning.
D. It stops the current loop immediately.

Solution

  1. Step 1: Understand the purpose of break

    The break statement is used to exit a loop early when a condition is met.
  2. Step 2: Compare options

    The description "It stops the current loop immediately." correctly describes break. "It skips the current iteration..." describes continue. Restarting or pausing are incorrect.
  3. Final Answer:

    It stops the current loop immediately. -> Option D
  4. Quick Check:

    break stops loop [OK]
Hint: Break stops loops immediately, no more iterations [OK]
Common Mistakes:
  • Confusing break with continue
  • Thinking break pauses instead of stops
  • Assuming break restarts the loop
2.

Which of the following is the correct syntax to break out of 2 nested loops in PHP?

for (...) {
    for (...) {
        // break here
    }
}
easy
A. break;
B. break(2);
C. break 2;
D. break 1;

Solution

  1. Step 1: Recall break syntax with levels

    To exit multiple nested loops, PHP uses break n; where n is the number of loops to break out of.
  2. Step 2: Identify correct syntax

    break 2; correctly breaks out of 2 loops. break(2); is invalid syntax. A plain break; or break 1; breaks only one loop.
  3. Final Answer:

    break 2; -> Option C
  4. Quick Check:

    break 2; exits 2 loops [OK]
Hint: Use break n; to exit n nested loops in PHP [OK]
Common Mistakes:
  • Using break(2) which is invalid syntax
  • Forgetting to specify number for multiple loops
  • Using break 1; when 2 loops need exit
3.

What is the output of this PHP code?

$count = 0;
for ($i = 0; $i < 3; $i++) {
    for ($j = 0; $j < 3; $j++) {
        $count++;
        if ($i + $j == 2) {
            break 2;
        }
    }
}
echo $count;
medium
A. 5
B. 3
C. 4
D. 6

Solution

  1. Step 1: Trace nested loops and break condition

    Loops run with i and j from 0 to 2. The break triggers when i + j == 2, breaking out of both loops immediately. count++ occurs before the check.
  2. Step 2: Simulate iterations

    - i=0,j=0: count=1 (sum=0 !=2)
    - i=0,j=1: count=2 (sum=1 !=2)
    - i=0,j=2: count=3 (sum=2 ==2), break 2; loops stop
    Total count = 3.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    3 increments before break 2 exits both loops [OK]
Hint: Count increments before break; break n exits n loops [OK]
Common Mistakes:
  • Counting increments after break condition
  • Ignoring break level effect
  • Assuming break only exits inner loop
4.

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";
    }
}
medium
A. break 3; is invalid because only 2 loops exist.
B. break 3; should be break 2; to exit both loops.
C. break 3; causes infinite loop.
D. No error, code runs fine.

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]
Hint: 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
5.

You want to stop processing nested loops early when a condition is met, but only exit the inner loop sometimes and both loops other times. Which code snippet correctly uses break with levels?

for ($i = 0; $i < 5; $i++) {
    for ($j = 0; $j < 5; $j++) {
        if ($i == 2 && $j == 3) {
            break 2; // exit both loops
        } elseif ($j == 4) {
            break; // exit inner loop only
        }
        echo "$i,$j\n";
    }
}
hard
A. Correct use of break with levels to control loop exit.
B. break 2; should be break 1; to exit both loops.
C. break; cannot be used inside nested loops.
D. break 2; exits only the inner loop.

Solution

  1. Step 1: Understand break with levels usage

    break 2; exits two loops, break; exits only the current loop.
  2. Step 2: Analyze code logic

    When $i==2 && $j==3, both loops exit with break 2;. When $j==4, only inner loop exits with break;. This matches the requirement.
  3. Final Answer:

    Correct use of break with levels to control loop exit. -> Option A
  4. Quick Check:

    break n exits n loops, break exits current loop [OK]
Hint: Use break n; for multiple loops, break; for single loop exit [OK]
Common Mistakes:
  • Confusing break 2; with break 1;
  • Thinking break; exits all loops
  • Misusing break levels causing logic errors
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