PHP continue Statement : A Tutorial with Examples

The continue statement in PHP is used within loops to skip the current iteration and move directly to the next iteration. When continue is encountered, the remaining code in the loop for the current iteration is skipped, and the loop proceeds to the next iteration.

The continue statement is useful when you want to skip certain iterations based on specific conditions without exiting the loop entirely.

In this tutorial, we will cover:

Let’s dive into each of these concepts with examples and explanations.

1. What is the continue Statement?

The continue statement is used to skip the rest of the code inside the current iteration of a loop and move directly to the next iteration. It does not terminate the loop like the break statement, but instead, it continues with the next iteration of the loop.

Syntax:

continue;

You can also specify an optional argument to indicate how many levels of loops to skip when using nested loops:

continue [n];  // Optional argument to skip to the next iteration of a specific level of nested loops

2. Using continue in Loops

The continue statement can be used in loops (e.g., for, while, foreach) to skip the current iteration and proceed to the next iteration, based on a condition.

Example: Using continue in a for Loop

<?php
for ($i = 1; $i <= 5; $i++) {
    if ($i == 3) {
        continue;  // Skip the iteration when $i equals 3
    }
    echo "Iteration: $i\n";
}
?>

Output:

Iteration: 1
Iteration: 2
Iteration: 4
Iteration: 5
  • In this example, the loop skips the iteration where $i equals 3, and continues with the next iteration.

3. Examples of continue in for, while, and foreach Loops

Example 1: continue in a while Loop

<?php
$count = 0;

while ($count < 5) {
    $count++;
    
    if ($count == 3) {
        continue;  // Skip iteration when $count equals 3
    }
    
    echo "Count: $count\n";
}
?>

Output:

Count: 1
Count: 2
Count: 4
Count: 5
  • In this example, the loop skips the output for the iteration where $count equals 3.

Example 2: continue in a foreach Loop

<?php
$fruits = ["Apple", "Banana", "Cherry", "Date"];

foreach ($fruits as $fruit) {
    if ($fruit == "Banana") {
        continue;  // Skip iteration for "Banana"
    }
    echo "Fruit: $fruit\n";
}
?>

Output:

Fruit: Apple
Fruit: Cherry
Fruit: Date
  • In this example, the foreach loop skips the iteration where the fruit is “Banana” and proceeds with the other elements.

4. Using continue in Nested Loops

In nested loops, the continue statement affects the current loop it is placed in. You can also use an optional argument with continue to specify which loop level should skip its iteration.

Example: Using continue in Nested Loops

<?php
for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        if ($j == 2) {
            continue 1;  // Skip the current iteration of the inner loop
        }
        echo "i = $i, j = $j\n";
    }
}
?>

Output:

i = 1, j = 1
i = 1, j = 3
i = 2, j = 1
i = 2, j = 3
i = 3, j = 1
i = 3, j = 3
  • In this example, the continue 1 skips the iteration of the inner loop when $j equals 2.

Example: Using continue to Skip Multiple Loop Levels

<?php
for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        if ($j == 2) {
            continue 2;  // Skip the iteration of both loops when $j equals 2
        }
        echo "i = $i, j = $j\n";
    }
}
?>

Output:

i = 1, j = 1
i = 2, j = 1
i = 3, j = 1
  • In this example, the continue 2 skips the current iteration of both the inner and outer loops when $j equals 2, resulting in skipping to the next iteration of the outer loop.

5. Common Use Cases for continue

The continue statement is commonly used in the following scenarios:

  • Skipping specific elements in loops: For example, skipping certain elements in an array that don’t meet a condition.
  • Skipping invalid data: When processing data, continue can be used to skip invalid or unwanted data.
  • Skipping to the next iteration in nested loops: When you need to bypass the current iteration of a nested loop.

Example: Skipping Odd Numbers

<?php
for ($i = 1; $i <= 10; $i++) {
    if ($i % 2 != 0) {
        continue;  // Skip odd numbers
    }
    echo "Even number: $i\n";
}
?>

Output:

Even number: 2
Even number: 4
Even number: 6
Even number: 8
Even number: 10
  • In this example, the continue statement is used to skip odd numbers, so only even numbers are printed.

Example: Skipping Empty Strings

<?php
$strings = ["Hello", "", "World", "", "PHP"];

foreach ($strings as $str) {
    if (empty($str)) {
        continue;  // Skip empty strings
    }
    echo "String: $str\n";
}
?>

Output:

String: Hello
String: World
String: PHP
  • In this example, the continue statement skips over any empty strings in the array.

6. Difference Between continue and break

continue:

  • Skips the current iteration of the loop and moves to the next iteration.
  • Does not exit the loop entirely, but only skips the current iteration.

break:

  • Exits the loop completely, ending all further iterations.
  • Used when you want to stop the loop execution entirely based on a condition.

Example: continue vs. break

<?php
for ($i = 1; $i <= 5; $i++) {
    if ($i == 3) {
        continue;  // Skip the iteration when $i equals 3
    }
    if ($i == 4) {
        break;  // Exit the loop when $i equals 4
    }
    echo "Iteration: $i\n";
}
?>

Output:

Iteration: 1
Iteration: 2
Iteration: 4
  • In this example, continue skips the iteration when $i is 3, and break exits the loop when $i is 4.

Summary of PHP continue Statement:

Concept Description
Basic Syntax continue; skips the current iteration and moves to the next iteration.
Using in Loops continue can be used in for, while, and foreach loops to skip specific iterations.
Using in Nested Loops You can use continue [n]; to skip specific levels of nested loops.
Common Use Cases Skipping elements in arrays, processing valid data only, skipping iterations in nested loops.
Difference from break continue skips the current iteration, while break exits the loop entirely.

Conclusion

The continue statement in PHP is a useful tool for controlling loop execution when you need to skip specific iterations based on certain conditions. In this tutorial, we covered:

  • The syntax of the continue statement and how it works in different types of loops.
  • Examples of using continue in for, while, and foreach loops.
  • How to use continue in nested loops to skip iterations in specific loop levels.
  • Common use cases where continue is helpful, such as skipping invalid data or filtering elements.
  • The difference between continue and break and when to use each.

Related posts

PHP Static Methods: A Tutorial with Examples

PHP Deleting Files: A Tutorial with Examples

PHP Copying Files: A Tutorial with Examples