PHP continue Statement: How to Skip Steps in Iteration

php -continue statement

The PHP continue statement skips the rest of the loop in the current cycle. It jumps to the next cycle in the loop.

Developers use it to skip invalid input. They also use it to filter data without extra conditions. It also helps avoid deep nesting.

Understand the continue Statement in PHP

The PHP continue statement forces the loop to move to the next cycle. It stops the current cycle early.

Here is the syntax:

continue;

You can write it like this to move on to the next loop cycle.

You can also use a numeric argument.

continue 2;

This is used to jump out of nested loops.

Here is how to jump based on the number of loops:

  • continue; — This skips the rest of the current iteration and starts the next iteration of the same loop.
  • continue 2; — This skips the rest of this iteration and continues at the next iteration of the outer loop.
  • continue 3; — The skips rest here and continue the next iteration three levels up in nested loops.
PHP continue Statement

It starts when you check a condition. It stops the current loop if true, then moves to the next cycle.

Here is a quick example:

for($inc = 1; $inc <= 10; $inc++) {
  
  if( $inc == 8 ) { // Skip this iteration
    continue;
  }
  
  echo "The number is $inc \n";
  
}

Here is the output:

The number is 1 
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 9
The number is 10

This figure shows the difference between loops with the continue statement and loops without it.

how continue works in php loops

The continue 2 statement is used to skip the rest of the current cycle in two nested loops. It tells the outer loop to move to its next cycle.

for ($i = 0; $i < 3; $i++) {
    for ($j = 0; $j < 3; $j++) {
        if ($j == 1) { 
            // Skip this iteration for the two loops
            continue 2;
        }
        echo "$i, $j\n";
    }
}

The output:

0, 0
1, 0
2, 0

This skips the inner loop and moves the outer loop to the next cycle.

Here are the use cases:

  • You skip over unwanted values in a loop.
  • It avoids extra nested checks.
  • It stops the current iteration early and moves execution to the next loop cycle.

Use the continue Statement with Other Loops

You can use continue in for loops to skip cycles that do not meet a rule. For example:

for ($i = 0; $i < 5; $i++) {
    // Skip this iteration
    if ($i == 2) { 
        continue; 
    }
    echo $i;
}

Output:

0134

It skips 2.

Use continue in foreach loops to skip over unwanted values. Here is an example:

For example:

$items = [1, 2, 3, 4];
foreach ($items as $item) {
    if ($item == 3) { // Skip this iteration
        continue;
    }
    echo $item;
}

Here is the output:

124

It skips 3.

You can use continue in while loops to control flow without extra nesting. Here is an example:

$i = 0;
while ($i < 5) {
    $i++;
    if ($i == 3) { // Skip this iteration
        continue;
    }
    echo $i;
}

Output:

1245

It skips 3.

You use continue in do…while loops the same way. For example:

$i = 0;
do {
    $i++; 
    if ($i == 4) { // Skip this iteration
        continue;
    }
    echo $i;
} while ($i < 5);

The output:

1235

It skips 4.

Examples

Skip even numbers in a for loop:

for ($i = 1; $i <= 5; $i++) {
    if ($i % 2 == 0) { // Skip this iteration
        continue;
    }
    echo $i;
}

The output:

135

This prints only odd numbers. It checks if $i is even. It uses continue to skip even numbers.

Skip null values in an array:

$values = [1, null, 2, null, 3];
foreach ($values as $value) {
    if ($value === null) { // Skip this iteration
        continue;
    }
    echo $value;
}

This prints 1, 2, 3. It skips null.

Nested loop with continue 2:

for ($i = 0; $i < 3; $i++) {
    for ($j = 0; $j < 3; $j++) {
        if ($j == 1) {
            continue 2;
        }
        echo "$i, $j\n";
    }
}

The output:

0, 0
1, 0
2, 0

It skips the inner and outer loop cycles. The outer loop moves ahead. It avoids unwanted pairs.

Filter strings in a list:

$words = ["apple", "", "banana", " ", "cherry"];
foreach ($words as $word) {
    if (trim($word) === "") { // Skip this iteration
        continue;
    }
    echo $word;
}

Here is the output:

applebananacherry

It removes empty or space-only strings by using trim().

Skip invalid CSV rows:

$rows = [
    ["id" => 1, "name" => "John"],
    ["id" => null, "name" => ""], // invalid
    ["id" => 2, "name" => "Sara"],
];

foreach ($rows as $row) {
    if (empty($row['id']) || empty($row['name'])) {
        continue; // skip invalid rows
    }
    echo "ID: {$row['id']} Name: {$row['name']}\n";
}

This code skips the invalid row in the array because the property ‘name’ has an empty value.

Wrapping Up

In this article, you learned what the PHP continue statement does and how it works in different loops.

Here is a quick recap.

  • The PHP continue statement lets a loop skip the rest of the current cycle and move to the next one.
  • You can use it for loop control to skip bad data like null values, empty strings, or failed responses.
  • It helps a loop skip cycle steps inside for, foreach, while, or do…while loops.
  • In nested loops, continue 2; or continue 3; moves control to an outer loop and skips deeper levels.
  • Real uses include skip invalid CSV rows, blank form fields, or failed API calls before more work.
  • The PHP continue statement with conditions stops extra checks after a loop cycle.

FAQs

What does the PHP continue statement do?

It stops the current loop cycle. It jumps to the next cycle immediately. Here is the syntax:
continue;

How does continue 2 work in PHP?

It skips two levels of loops. It tells the outer loop to move to its next cycle.

Can I use continue in all loops?

Yes. You can use it in for, foreach, while, and do...while loops.

What is the syntax for the continue statement?

Use continue; for one level. Use continue 2; for two levels.

Why use the PHP continue statement?

It controls loop flow. It skips unwanted cycles. It avoids extra code checks.

Similar Reads

Get Last Insert ID in PHP with MySQL PDO and MySQLi

Keeping track of the last record inserted is often important. Whether you are managing user registrations, processing orders, or handling…

PHP array_fill_keys: Create Arrays with Specific Keys

The PHP array_fill_keys function helps you to build an array where each key has the same value. What is the…

PHP htmlspecialchars Function: Prevent XSS in HTML Output

A PHP script can break the page or allow code injection if it outputs user input directly into HTML. The…

PHP strict_types: How the Strict Mode Works

If you want to write good PHP code, strict mode should be on your radar. Strict mode—activated by the command declare(strict_types=1); at…

PHP array_column: How to Extract Values from Arrays

The array_column function in PHP takes values from one column in a multidimensional array. You can use it to pull…

PHP array_key_first Function: How it Works with Examples

The array_key_first helps you to get the first key of an array directly in PHP. It saves time and removes…

PHP str_replace Function: How it Works with Examples

If you find a word that does not fit and want to fix it. You can use the PHP str_replace…

PHP MySQL LIMIT Data: How to Optimize PHP Queries?

There are situations where you only need a specific number of rows returned. This is where the "LIMIT" clause in…

PHP Math: Essential Functions with Examples

PHP math may sound like a pretty simple thing, but I assure you it's a set of tools that will…

PHP Hello World: Write Your First Program with Code Example

You always start with “Hello World” when you learn a new language. It keeps things simple. You see right away…

Previous Article

PHP Conditional Operator: How It Works with Examples

Next Article

PHP Loop: How Loops Work with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.