LearnContact
Lesson 2518 min read

Break, Continue & Goto

Learn how to control loop execution in PHP with break and continue, including their numeric forms for nested loops, and why goto is rarely used.

Introduction

Loops usually run to completion on their own, but sometimes you need finer control — stopping early once you have found what you need, or skipping an iteration that does not apply. PHP gives you break and continue for exactly this, plus a rarely-used goto statement inherited from older procedural languages.

In this lesson you will learn how break exits a loop (or switch) early, how continue skips to the next iteration, how both support a numeric argument for controlling nested loops, and why goto is generally best avoided in modern PHP.

What You Will Learn
  • How break exits a loop or switch immediately.
  • How break 2 exits out of two levels of nested loops at once.
  • How continue skips the rest of the current iteration.
  • How continue 2 works with nested loops.
  • What goto does, and why modern PHP code rarely uses it.

The break Statement

break immediately stops the innermost loop (or switch) it is inside, and execution continues with the code right after that loop.

break-basic.php
<?php
for ($i = 1; $i <= 10; $i++) {
    if ($i === 5) {
        break;
    }
    echo "$i ";
}
echo "\nLoop stopped early.";
Output
1 2 3 4 
Loop stopped early.

This is useful for stopping as soon as you find what you are looking for, instead of wastefully continuing to check every remaining item.

break-search.php
<?php
$numbers = [4, 9, 15, 22, 31, 40];

foreach ($numbers as $number) {
    if ($number > 20) {
        echo "Found the first number over 20: $number";
        break;
    }
}
Output
Found the first number over 20: 22

Breaking Out of Nested Loops

By default, break only exits the innermost loop it is inside. To break out of an outer loop as well, give break a numeric argument specifying how many levels to exit — break 2 exits two nested loops at once.

break-2.php
<?php
for ($row = 1; $row <= 3; $row++) {
    for ($col = 1; $col <= 3; $col++) {
        if ($row === 2 && $col === 2) {
            echo "Stopping both loops at ($row,$col)\n";
            break 2;
        }
        echo "($row,$col) ";
    }
}
Output
(1,1) (1,2) (1,3) (2,1) Stopping both loops at (2,2)

The continue Statement

continue skips the rest of the current iteration and jumps straight to the next one — the loop keeps running, it just moves on early for that particular pass.

continue-basic.php
<?php
for ($i = 1; $i <= 6; $i++) {
    if ($i % 2 === 0) {
        continue;
    }
    echo "$i is odd\n";
}
Output
1 is odd
3 is odd
5 is odd

Here, every even number is skipped by continue before the echo statement runs, so only odd numbers get printed.

Continuing in Nested Loops

Just like break, continue can take a numeric argument. continue 2 skips the rest of the current iteration of the inner loop and also the rest of the current iteration of the outer loop, moving straight to the outer loop's next pass.

continue-2.php
<?php
for ($row = 1; $row <= 3; $row++) {
    for ($col = 1; $col <= 3; $col++) {
        if ($col === 2) {
            continue 2;
        }
        echo "($row,$col) ";
    }
}
Output
(1,1) (2,1) (3,1) 

As soon as $col reaches 2, continue 2 abandons the rest of that inner loop iteration and moves the outer loop to its next row, which is why only the first column of each row gets printed.

The goto Statement

goto jumps execution directly to a labeled point elsewhere in the same script. A label is just an identifier followed by a colon, and goto label; transfers control there immediately.

goto-basic.php
<?php
$i = 1;

start:
echo "$i ";
$i++;

if ($i <= 5) {
    goto start;
}

echo "\nDone.";
Output
1 2 3 4 5 
Done.

This example re-implements a simple counting loop using goto instead of while — and it immediately shows why goto is a poor substitute: the same result is far clearer written as a normal loop.

Why goto Is Discouraged

goto lets execution jump anywhere a label exists, which makes it easy to create code whose flow is difficult to trace — sometimes called "spaghetti code." Structured control flow (loops, functions, break, continue) expresses the same intent far more clearly and is what virtually all modern PHP style guides and codebases use instead.

When (If Ever) to Use goto

goto is essentially never needed in application code. Its only occasional legitimate use is breaking out of deeply nested loops in older code where break N would be awkward — and even then, refactoring into a function with an early return is almost always clearer.

Common Mistakes

Avoid These Mistakes
  • Forgetting that break/continue without a number only affects the innermost loop.
  • Using break inside a switch that is nested in a loop and expecting it to exit the loop — it only exits the switch (use break 2 for the loop).
  • Reaching for goto to escape nested loops when break N is clearer and safer.
  • Overusing continue to the point where the loop body becomes hard to follow — prefer restructuring the condition instead.

Best Practices

  • Use break to exit a loop as soon as further iteration is pointless.
  • Use continue to skip iterations that do not apply, keeping the "happy path" code unindented.
  • Prefer break N / continue N over goto when working with nested loops.
  • Avoid goto in application code; reach for functions, early returns, and normal control structures instead.

Frequently Asked Questions

Does break work inside a switch statement?

Yes — inside a switch, break exits the switch itself. If the switch is nested inside a loop and you want to exit the loop, use break 2.

Can continue be used in a switch statement?

Using continue inside a switch behaves like break for that switch in older PHP, but this is confusing and deprecated; always use break inside switch instead.

Is goto ever necessary in modern PHP?

Almost never. It exists for rare legacy cases, but structured alternatives like break N, functions, and early returns cover virtually every situation more clearly.

What happens if I use break 3 but there are only 2 nested loops?

PHP raises a fatal error, since there is no third enclosing loop or switch to break out of.

Key Takeaways

  • break exits the current loop or switch immediately.
  • break 2 (or higher) exits multiple levels of nested loops at once.
  • continue skips the rest of the current iteration and moves to the next one.
  • continue 2 (or higher) applies that skip at an outer loop level.
  • goto jumps to a labeled point in the script but is discouraged in favor of clearer control structures.

Summary

break and continue give you precise control over loop execution — stopping early or skipping ahead — and both support numeric arguments for working with nested loops. goto offers similar jumping power but at the cost of readability, which is why modern PHP code relies on structured control flow instead.

Lesson 25 Completed
  • You can use break and continue to control loop execution.
  • You understand break 2 and continue 2 for nested loops.
  • You know what goto does and why it is rarely used today.
  • You are ready to learn how to define and use functions.
Next Lesson →

Functions