← Back to PHP Course | Chapter 4: Control Flow | Lesson 14 of 14

PHP break & continue

break is an emergency exit that ends a loop right away, while continue skips only the current round and moves on to the next one. It is like leaving the game versus just skipping your turn.
Syntax
php
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
<?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";
}
?>

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
<?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";
}
?>

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
<?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";
    }
}
?>

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
<?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";
    }
}
?>

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
<?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;
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using break when continue is intended, which exits the whole loop instead of skipping one pass.
  2. Forgetting that break exits only the innermost loop; use break 2; to leave two nested loops.
  3. Using continue in a while loop after skipping the increment line, causing an infinite loop.
Chapter Summary
  • if, if-else, elseif, nested conditions, switch, and match choose between paths.
  • while, do-while, for, and foreach loops repeat code.
  • break and continue control loop flow.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.