PHP break & continue
In this page:
break; // exit the loop
continue; // skip to the next iteration
break 2; // exit two nested loops
The break Statement
break exits the loop it's inside immediately, skipping any remaining iterations entirely and jumping straight to the first line of code after the loop — useful the moment you've found what you were searching for and don't need to keep checking further elements.
Example: The break Statement
<?php
// Declare `$numbers` as an array: `[1, 5, 3, 8, 2]`
$numbers = [1, 5, 3, 8, 2];
// Loop over `$numbers`, binding each item to `$n`
foreach ($numbers as $n) {
// Check whether `$n == 8`
if ($n == 8) {
break;
}
// Print `$n . "\n"` to the output
echo $n . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
The continue Statement
continue skips only the rest of the *current* iteration and jumps straight to the loop's next pass, without exiting the loop altogether the way break does — the right choice when you want to ignore one specific case but keep processing everything after it.
Example: The continue Statement
<?php
// Classic for-loop: `$i = 1; $i <= 5; $i++`
for ($i = 1; $i <= 5; $i++) {
// Check whether `$i == 3`
if ($i == 3) {
continue;
}
// Print `$i . "\n"` to the output
echo $i . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
break with Multi-level Loops
break accepts an optional integer argument specifying how many nested loop levels to exit at once — break 2; from inside a doubly-nested loop exits both loops in one statement, instead of only escaping the innermost one.
Example: break with Multi-level Loops
<?php
// Classic for-loop: `$i = 1; $i <= 2; $i++`
for ($i = 1; $i <= 2; $i++) {
// Classic for-loop: `$j = 1; $j <= 2; $j++`
for ($j = 1; $j <= 2; $j++) {
// Check whether `$j == 2`
if ($j == 2) {
break 2;
}
// Print "$i,$j\n" to the output
echo "$i,$j\n";
}
}
?>
Login to try C/C++/Java/PHP code in the editor
continue with Multi-level Loops
continue accepts the same kind of numeric argument as break does, letting you skip straight to the next iteration of an *outer* loop from deep inside a nested one, rather than only advancing the innermost loop's own iteration.
Example: continue with Multi-level Loops
<?php
// Classic for-loop: `$i = 1; $i <= 2; $i++`
for ($i = 1; $i <= 2; $i++) {
// Classic for-loop: `$j = 1; $j <= 2; $j++`
for ($j = 1; $j <= 2; $j++) {
// Check whether `$j == 1`
if ($j == 1) {
continue 2;
}
// Print "$i,$j\n" to the output
echo "$i,$j\n";
}
}
?>
Login to try C/C++/Java/PHP code in the editor
break in switch Blocks
PHP treats switch as a loop-like structure for the purposes of break, which is exactly why every case needs its own break — without one, execution keeps falling through into the next case's code instead of exiting the switch block.
Example: break in switch Blocks
<?php
// Declare `$day`, set to "Mon"
$day = "Mon";
// Switch on `$day` and branch by matching case
switch ($day) {
// Case: `"Mon"`
case "Mon":
// Print "Monday" to the output
echo "Monday";
break;
// Case: `"Tue"`
case "Tue":
// Print "Tuesday" to the output
echo "Tuesday";
break;
}
?>
Login to try C/C++/Java/PHP code in the editor
- Using
breakwhencontinueis intended, which exits the whole loop instead of skipping one pass. - Forgetting that
breakexits only the innermost loop; usebreak 2;to leave two nested loops. - Using
continuein awhileloop after skipping the increment line, causing an infinite loop.
if,if-else,elseif, nested conditions,switch, andmatchchoose between paths.while,do-while,for, andforeachloops repeat code.breakandcontinuecontrol loop flow.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: