break and continue both change a loop's normal flow, but in genuinely different ways — mixing them up is a common source of off-by-one logic bugs.
break — exits the loop entirely
for ($i = 0; $i < 10; $i++) {
if ($i === 5) {
break;
}
echo $i; // prints 0 1 2 3 4
}
Once break runs, the loop stops immediately — no further iterations happen, even if the loop's normal condition would still be true.
continue — skips just the current iteration
for ($i = 0; $i < 10; $i++) {
if ($i % 2 === 0) {
continue;
}
echo $i; // prints 1 3 5 7 9
}
continue jumps straight to the loop's next iteration (re-checking the condition, running any increment step) without executing the rest of the current iteration's code — the loop keeps running, just skipping specific passes.
Breaking out of nested loops
Both break and continue accept an optional numeric argument specifying how many levels of enclosing loop structure to break out of or continue:
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
if ($j === 1) {
break 2; // breaks out of BOTH loops, not just the inner one
}
echo "$i,$j ";
}
}
Without the 2, a plain break only exits the innermost loop, and the outer loop would continue iterating — this is a real, easy-to-miss bug when nested loops are involved.
In switch statements
break is also what ends a case in a switch statement — omitting it causes "fall-through" into the next case, which is sometimes intentional but is a classic source of bugs when it's accidental. continue inside a switch that's itself inside a loop behaves like break for the switch (PHP treats a bare switch as one loop level for this purpose) — using continue 2 is the explicit way to actually continue the outer loop from inside a switch case.