php foreach continue

php foreach continue

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

php foreach continue

A frequent stumbling block in PHP logic involves the interaction between loop control statements and variable scope. Consider a scenario where you need to skip iterations based on specific conditions, such as mismatched route segments or missing parameter matches. You might find yourself initializing a flag variable inside the loop, like $continue, only to see the loop behave unexpectedly, seemingly ignoring your logic entirely.

In the provided code snippet, the developer initializes $continue = 0 at the start of every foreach iteration. This is a classic anti-pattern that masks the intent of the control flow. The core issue lies in the placement of the continue statement relative to the loop header and variable initialization. When you write:

foreach ($this->routes as $route => $path) {
    $continue = 0; // Reset every time
    
    // ... logic ...

    if (count($route_segments) != $count) {
        $continue = 12;
        continue; // This stops the current iteration immediately
    }
    
    // Inner loop checks...
}

The continue statement functions exactly as its name suggests: it halts execution of the current iteration and jumps directly to the next iteration of the outer foreach loop. The variable $continue is indeed being set, but assigning a value to it after the continue statement has already executed is functionally irrelevant for that specific cycle. The code dies immediately after printing $continue, which is why we see the output (12 or 34) and stop processing further routes.

However, if the goal was to accumulate logic across multiple iterations or use that flag outside the loop, resetting it inside the loop prevents any state accumulation. If the intention is simply to filter out invalid routes, relying on a boolean flag like $continue is unnecessary overhead. Modern PHP best practices favor returning early from functions or breaking loops when a condition is met, rather than setting flags that must be checked later.

Refactoring for Clarity

To address the specific logic of skipping routes where segment counts do not match, you can remove the intermediate flag entirely. Instead of assigning a value to $continue and then calling continue, simply execute the statement directly:

foreach ($this->routes as $route => $path) {
    if (count($route_segments) != $count) {
        continue; // Skip this iteration immediately
    }

    foreach ($i = 0; $i < $count; $i++) {
        if ($route_segments[$i] != $segments[$i] && !preg_match('/^\x24[0-9]+$/', $route_segments[$i])) {
            continue; // Skip to next iteration of the inner loop
        }
    }

    // If we reach here, conditions are met
    echo "Match found: $path";
}

In this refactored approach, the continue statement acts as a gatekeeper. If the condition inside the if block evaluates to true, execution jumps straight to the next element in $this->routes. The inner for loop follows the same principle; if a segment mismatch occurs that isn't a numeric parameter (checked via regex), the inner loop skips to the next index.

Why Flags Often Fail Here

Developers sometimes prefer flags like $continue = 12 because they want to debug why a skip happened by inspecting the variable later. However, this creates "dead code" paths. Once continue executes, the lines following it in the same iteration are unreachable unless you use labels or complex control structures, neither of which is needed here.

If you need to exit the entire loop rather than just skipping one iteration, use break. If you need to skip an iteration, use continue. Mixing these concepts with temporary variables often leads to confusion about whether the loop has actually progressed or if the condition was truly met.

For projects requiring robust routing logic, similar patterns are common in frameworks like Laravel, where route matching is handled internally to ensure consistency. Understanding these low-level control structures helps when building custom middleware or parsers that need to traverse complex data structures without relying on framework abstractions.

Our network