Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The continue statement is a control flow structure used within loop constructs (for, foreach, while, do-while) to immediately halt the execution of the current iteration. Upon encountering continue, the PHP engine skips any remaining statements in the current loop body and advances execution to the loop’s next iteration boundary.
continue;
// or
continue <level>;

Execution Mechanics by Loop Type

The exact behavior of the iteration boundary depends on the enclosing loop structure:
  • while and do-while loops: Execution jumps directly to the conditional expression to determine if the next iteration should proceed.
  • for loops: Execution jumps to the loop’s step expression (e.g., $i++), evaluates it, and then proceeds to the conditional expression.
  • foreach loops: Execution advances the internal iteration state to the next element and evaluates if elements remain. (Note: Modern PHP uses an independent internal state for foreach iteration, not the array’s internal pointer).

Level Arguments

The continue statement accepts an optional argument (<level>) that dictates how many nested enclosing loop structures it should skip to the end of. This argument must be a positive integer literal (greater than 0). The default value is 1, which targets the immediate enclosing loop. Passing 0 results in a Fatal Error. Passing negative numbers (e.g., continue -1;) results in a Parse Error (syntax error, unexpected token "-") because the PHP grammar strictly expects a positive integer literal (T_LNUMBER) and cannot parse the minus sign in this context. As of PHP 8.0, passing variables as the level argument (e.g., continue $var;) results in a Parse Error (syntax error, unexpected variable).
while ($conditionA) {
    while ($conditionB) {
        // Halts the current iteration of the inner loop, 
        // AND halts the current iteration of the outer loop,
        // jumping directly to the evaluation of $conditionA.
        continue 2; 
    }
}

Behavior within switch Statements

In PHP, the switch statement is historically classified as a looping structure for the purposes of continue. Previously, executing continue (or continue 1) inside a switch behaved identically to break, terminating the switch block without affecting any outer loops. However, as of PHP 8.0, using continue to target a switch statement results in a Fatal Error (Compile Error), meaning the script will fail to compile entirely. To terminate a switch block, break must be used.
// Invalid usage (PHP 8.0+)
switch ($variable) {
    case 'A':
        // Throws a Fatal Error (Compile Error).
        // 'break;' must be used here to exit the switch block.
        continue;   
}
To skip an iteration of a loop that encloses a switch statement, a level argument of 2 or higher must be explicitly declared. This bypasses the switch and correctly targets the enclosing loop.
// Valid usage targeting an outer loop
while ($condition) {
    switch ($variable) {
        case 'B':
            // Exits the switch AND skips the remainder of the while loop iteration.
            // This is valid and executes without error.
            continue 2; 
    }
}
Master PHP with Deep Grasping Methodology!Learn More