PHP Looping with the Break Statement
In PHP, looping through arrays or repeating a block of code a certain number of times is a common task. The break statement provides an efficient way to exit a
When you write a loop in PHP, you often need finer control than "run every iteration." Two statements give you that control: break exits the loop early, and continue skips the rest of the current iteration. This page covers both, including how to apply them to nested loops with break N and continue N.
Understanding the Break Statement
The break statement terminates the current loop (or switch) immediately and transfers control to the first statement after the loop. Use it when you have found what you were looking for, or when carrying on would be pointless or unsafe — for example, after spotting a matching record or hitting an error condition.
break works inside for, foreach, while, and do...while loops, as well as inside a switch statement.
Note: Using
breakoutside of a loop orswitchstatement triggers a fatal error in PHP.
Using the Break Statement in a For Loop
In a for loop, the break statement can be used to stop the loop when a specific condition is met. For example, the following code uses a for loop to search for a number in an array:
PHP example of break in for loop
In this code, the loop will stop as soon as the value of $numbers[$i] is equal to 3. The output of this code will be "Found 3 at index 2".
Using the Break Statement in a Foreach Loop
In a foreach loop, the break statement works similarly to the for loop. The following code uses a foreach loop to search for a specific value in an associative array:
PHP Example of break in foreach loop
In this code, the loop will stop as soon as the key $color is equal to "green". The output of this code will be "green has hex code #00FF00".
Breaking Out of Nested Loops
The break statement can also be used to break out of nested loops. When a break statement is used inside a nested loop, only the innermost loop is terminated.
For example, the following code uses two nested loops to search for a specific value in a two-dimensional array:
PHP example of nested loops break
In this code, the break 2 statement is used to exit two levels of nesting. The output of this code will be Found 5 at [1][1].
The number after break is how many enclosing loops to break out of, not a loop index. break 1 is the same as a plain break, and the number cannot be larger than the actual nesting depth.
Understanding the Continue Statement
Where break leaves the loop entirely, continue only skips the rest of the current iteration and jumps straight to the next one. The loop itself keeps running. This is the natural choice when you want to ignore some items but still process everything else.
The following loop prints every number from 1 to 10 except the even ones:
PHP example of continue in a for loop
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 == 0) {
continue; // skip even numbers
}
echo "$i ";
}
?>The output is 1 3 5 7 9 — when $i is even, continue skips the echo and the loop moves on to the next value.
Continue in a Foreach Loop
continue is especially handy in a foreach loop when you want to filter items as you go. Here we skip any product that is out of stock:
PHP example of continue in a foreach loop
<?php
$products = array(
array("name" => "Pen", "stock" => 12),
array("name" => "Notebook", "stock" => 0),
array("name" => "Eraser", "stock" => 5),
);
foreach ($products as $product) {
if ($product["stock"] === 0) {
continue; // don't list sold-out items
}
echo $product["name"] . " (" . $product["stock"] . " in stock)\n";
}
?>This prints Pen and Eraser, skipping the out-of-stock Notebook.
Skipping Multiple Levels with continue N
Just like break N, continue N skips to the next iteration of an outer loop. continue 2, for instance, restarts the loop one level up:
PHP example of continue 2 in nested loops
<?php
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= 3; $j++) {
if ($j == 2) {
continue 2; // jump to the next $i
}
echo "i=$i j=$j\n";
}
}
?>The inner loop only ever prints j=1, because reaching j == 2 restarts the outer loop. The output is i=1 j=1, i=2 j=1, i=3 j=1.
Break vs. Continue
| Statement | Effect |
|---|---|
break | Stops the loop completely; execution resumes after the loop. |
continue | Skips the rest of the current iteration; the loop continues. |
break N | Breaks out of N nested loops at once. |
continue N | Skips to the next iteration of the loop N levels up. |
Common Gotchas
continuein ado...whileloop still re-checks the condition. It does not skip the condition test, so the loop can still end after acontinue.continuebehaves differently insideswitch. Inside aswitchthat sits within a loop,continueacts likebreakfor theswitchand emits a warning in modern PHP. Usebreakto leave theswitch, and reservecontinuefor the surrounding loop.- Off-by-one with
continuein aforloop is rare because theforstatement's increment runs normally — but in awhileloop you must update the counter beforecontinue, or you can create an infinite loop.
Conclusion
The break and continue statements give you precise control over loop execution: break exits a loop early, while continue skips an iteration and keeps going. The break N and continue N forms extend that control across nested loops. Next, review how the different loop types work — for, foreach, while, and do...while.