ZetCode

PHP continue Statement

last modified November 25, 2026

The PHP continue statement skips the rest of the current loop iteration and moves to the next one. It is the tool for ignoring a value that does not need processing while keeping the loop running.

This article shows continue in for, while, foreach, and do-while, the numeric level that leaves nested loops, the special case inside switch, and the errors raised by misplaced or invalid statements.

Basic definitions

continue ends the current pass through the innermost for, foreach, while, or do-while loop. Execution jumps to the next step of that loop, not past the loop: a for runs its increment expression, a while and a do-while evaluate their condition, and a foreach advances to the next element.

An optional number sets how many enclosing loops to skip: continue 2; abandons the rest of the inner loop and the rest of the outer loop's current pass, then starts the outer loop's next iteration. The number must be a positive integer literal; a variable is rejected and continue 0 is invalid. Without a number the level is 1.

The difference from break is the destination. break leaves the loop entirely and continues after it, while continue keeps looping. The PHP break statement article covers the early exit.

Like break, continue is resolved when the file is compiled, so a statement outside a loop or switch is a fatal error rather than a warning.

continue in a for loop

The first example collects the odd numbers from 1 to 10 and skips the even ones.

continue_for.php
<?php

declare(strict_types=1);

$odd = [];

for ($i = 1; $i <= 10; $i++) {
    if ($i % 2 === 0) {
        continue;
    }

    $odd[] = $i;
}

echo implode(" ", $odd), "\n";

When $i is even, the condition sends control to the end of the body, so the value is never appended. The loop header still runs $i++, which is the key difference from a while loop that increments inside the body. The array is printed with implode() to avoid a trailing space.

$ php continue_for.php
1 3 5 7 9

continue in a while loop

A while loop does not have a header that advances the counter, so the increment must come before continue. Otherwise the same value is tested forever.

continue_while.php
<?php

declare(strict_types=1);

$n = 0;
$kept = [];

while ($n < 6) {
    $n++;

    if ($n === 3) {
        continue;
    }

    $kept[] = $n;
}

echo implode(" ", $kept), "\n";

$n++ runs before the check, so skipping 3 still leaves the counter at 3 and the next pass moves on to 4. If the increment sat after the continue, the loop would never reach 4 and would run without end. This placement rule is the most common source of bugs with continue in while loops.

$ php continue_while.php
1 2 4 5 6

continue in a foreach loop

Filtering a collection is the most natural use: the loop visits every record and skips the ones that do not qualify.

continue_foreach.php
<?php

declare(strict_types=1);

$users = [
    ["name" => "Alice", "active" => true],
    ["name" => "Bob", "active" => false],
    ["name" => "Cara", "active" => true],
    ["name" => "Dan", "active" => false],
];

foreach ($users as $user) {
    if (!$user["active"]) {
        continue;
    }

    echo "Active: ", $user["name"], "\n";
}

The condition is written as a guard at the top of the body: if the user is inactive, the remaining statements are skipped. The alternative is to wrap the whole body in if ($user["active"]), which adds a level of indentation for every filter. With several independent filters, a sequence of guards reads better than nested conditions. The PHP foreach loop article covers the loop itself.

$ php continue_foreach.php
Active: Alice
Active: Cara

continue in a do-while loop

A do-while checks its condition after the body, and continue jumps straight to that check. The loop still runs at least once, whatever the condition says.

continue_dowhile.php
<?php

declare(strict_types=1);

$n = 0;
$kept = [];

do {
    $n++;

    if ($n % 3 === 0) {
        continue;
    }

    $kept[] = $n;
} while ($n < 10);

echo implode(" ", $kept), "\n";

Multiples of 3 are skipped after the counter has been incremented, so the loop still terminates at 10. The output keeps 1, 2, 4, 5, 7, 8, and 10. As in the while example, the increment belongs before the guard; the condition is evaluated even when the rest of the body is abandoned. The do-while loop article explains the structure.

$ php continue_dowhile.php
1 2 4 5 7 8 10

Skipping to an outer loop

With nested loops, a level of 2 abandons the current inner pass and also skips the rest of the outer pass. The outer loop then starts its next iteration.

continue_level.php
<?php

declare(strict_types=1);

$pairs = [];

for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        if ($i === $j) {
            continue 2;
        }

        $pairs[] = "$i,$j";
    }
}

echo implode(" ", $pairs), "\n";

The inner loop builds pairs of $i and $j. When the two are equal, continue 2 leaves the inner loop and moves to the next $i, so the remaining $j values for that row are never visited. That is why the output keeps only the pairs below the diagonal: 2,1 3,1 3,2. A plain continue would have skipped just the diagonal cell and produced nine minus three pairs instead.

$ php continue_level.php
2,1 3,1 3,2

continue inside a switch

A switch is not a loop, and a plain continue inside one acts like break. PHP accepts the code but warns that the intent is probably a level of 2.

continue_switch.php
<?php

declare(strict_types=1);

$kept = [];

for ($i = 1; $i <= 3; $i++) {
    switch ($i) {
        case 2:
            continue;
        default:
            $kept[] = $i;
    }
}

echo implode(" ", $kept), "\n";

The warning reads "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? and points at the statement. The loop still prints 1 and 3, because the matching case breaks out of the switch and execution continues with the next loop iteration. To skip the rest of the loop pass from inside a switch, write continue 2.

$ php continue_switch.php
Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /app/continue_switch.php on line 10
1 3

The level counts the switch too

continue 2 from a case leaves the switch and finishes the loop pass, which is exactly what the warning suggests.

continue_switch_level.php
<?php

declare(strict_types=1);

$kept = [];

for ($i = 1; $i <= 3; $i++) {
    switch ($i) {
        case 2:
            continue 2;
        default:
            $kept[] = $i;
    }
}

echo implode(" ", $kept), "\n";

The output is the same as before, but the code is now explicit and warning-free: the switch counts as one level and the loop as the second. Use this form whenever a case has to skip the remaining work of the iteration. The PHP switch/case keywords article covers case labels and fall-through.

$ php continue_switch_level.php
1 3

continue outside a loop is an error

The statement needs a surrounding loop or switch, and the compiler rejects it when there is none.

continue_outside.php
<?php

declare(strict_types=1);

$total = 0;

continue;

echo $total, "\n";

PHP reports 'continue' not in the 'loop' or 'switch' context. A function body is its own scope, so a continue inside a function is invalid even when the function is called from a loop. Only a loop in the same scope can be skipped.

$ php continue_outside.php
Fatal error: 'continue' not in the 'loop' or 'switch' context in /app/continue_outside.php on line 7
Stack trace:
#0 {main}

The level must be a positive integer

The numeric argument is checked at compile time. Zero does not name a valid level, and a variable is not a constant level.

continue_zero.php
<?php

declare(strict_types=1);

for ($i = 0; $i < 3; $i++) {
    continue 0;
}

The message is 'continue' operator accepts only positive integers. A variable in the same position produces 'continue' operator with non-integer operand is no longer supported. Keeping the level a literal makes the control flow visible while reading the code.

$ php continue_zero.php
Fatal error: 'continue' operator accepts only positive integers in /app/continue_zero.php on line 6
Stack trace:
#0 {main}

Best practices

Use continue for a guard clause that rejects an item early, and keep the decision close to the top of the loop body.

The loop structures are covered in PHP flow control, PHP for loop, and PHP while loop.

Source

PHP manual: continue

PHP manual: for

PHP manual: while

This article covered the PHP continue statement: skipping iterations in every loop form, moving to an outer loop with a level, the switch special case, and the errors raised by invalid placement or levels. Continue with PHP break statement to leave a loop instead of skipping one pass, or read PHP switch/case keywords for case handling.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all PHP tutorials.