Continue Statement in PHP

The continue statement in PHP is also a control flow statement similar to break statement except that it only stops the current execution of loop instead of stopping the entire loop.

The continue statement is used to skip the current iteration and move to the next iteration of the loop. When the PHP interpreter encounters a continue statement inside a loop, it terminates the current iteration and moves directly to the next one, skipping the rest of the code in the loop body for the current iteration.

In other words, when the continue statement encountered, the control immediately transfers to the next iteration without executing any remaining code inside the loop for the current iteration.

Syntax of Continue Statement in PHP


The basic syntax of the continue statement in PHP is similar to the break statement. The syntax is as follows:

continue;

Here, continue is a keyword that skips the remaining code in the current loop iteration and moves to the next iteration.

Here is a simple example of the continue statement:

<?php
for ($i = 1; $i <= 5; $i++) {
   if ($i == 3) {
      continue; // Skip the current iteration when $i is 3.
   }
   echo "$i \n";
}
?>
Output:
      1
      2
      4
      5

In this example, when $i is equal to 3, the continue statement inside the if statement will execute and it will stop the current iteration by skipping the remaining code below and move to the next iteration. In this case, the output will be 1, 2, 4, and 5, skipping 3 entirely. Look at the execution style of continue statement in the below figure.

An example of execution style of continue statement in PHP.

You can use the continue statement to control the flow of execution within any loop construct, such as for, foreach, while, and do-while. The continue statement allows you to control over the loop execution based on the specific condition. Let’s use the continue statement within different loops.

Using Continue Statement in For Loop


When the continue statement executes inside a loop body, the current iteration of the loop ends and the next iteration immediately starts.

If you use the continue statement within a for loop, the control jumps back to its increment expression, executes it, and then checks the loop’s condition to determine whether to continue with the next iteration.

Example 1: Let’s write a PHP program to print odd numbers between 1 to 10 using the continue statement.

<?php
for ($i = 1; $i <= 10; $i++) {
   if ($i % 2 == 0) {
       continue; // Skip even numbers
   }
   echo $i . " ";
}
?>
Output:
      1 3 5 7 9

In this example, the continue statement skips all even numbers in the loop by checking if $i % 2 == 0.

Using Continue in while Loop


When you use the continue statement within a while loop, the control transfers back to re-evaluate the conditional expression inside the while statement. If the condition evaluates to true, the loop continues with the next iteration. Let’s take an example on it.

Example 2:

<?php
$i = 0;
while ($i < 5) {
   $i++;
   if ($i == 3) {
      continue; // Skips the current iteration when $i is equal to 3.
   }
   echo "Number: $i \n";
}
?>
Output:
      Number: 1
      Number: 2
      Number: 4
      Number: 5

Using Continue Statement in do-while Loop


When you use the continue statement within a do-while loop, the control skips the remaining code in the current iteration and starts the next iteration from the top of the loop body. However, this only happens if the conditional expression inside the while statement evaluates to true.

Example 3:

<?php
$i = 0;
do {
  $i++;
  if ($i == 3) {
      continue;
  }
  echo "Number: $i \n";
} while ($i < 5);
?>
Output:
      Number: 1 
      Number: 2 
      Number: 4 
      Number: 5 

Continue in foreach Loop


Example 4: Let’s write a PHP program to demonstrate the use of continue inside foreach loop.

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

foreach ($fruits as $fruit) {
   if ($fruit == "Cherry") {
     continue; // Skip "Cherry"
   }
   echo "$fruit\n";
}
?>
Output:
      Apple
      Banana
      Dates

Using Continue with Nested Loops


When you use the continue statement inside the nested loops, the continue statement stops only the current iteration of the innermost loop in which it is placed. It skips the remaining code within that inner loop and moves directly to the next iteration of the same inner loop. The outer loop remains unaffected by this continue statement, and its execution proceeds as expected.

Example 5:

<?php
// Outer loop.
for($i = 1; $i <= 3; $i++)
{
// Inner loop.
   for($j = 1; $j <= 3; $j++)
   {
     if($i == 2 && $j == 3)
        continue; // continue statement inside inner loop.
     echo $i . " ". $j . "\n";
   }
}
?>
Output:
      1 1
      1 2
      1 3

      2 1
      2 2
      3 1
      3 2
      3 3

In PHP, the continue statement can also accept an optional numeric argument to specify which loop to skip to in nested loops. The basic syntax is as:

continue n;

Here, the numeric argument n specifies the number of nested loops to skip to. If n is equal to 1, it skips the current iteration of the innermost loop. This is default.

The continue n skips to the next iteration of the outer loop, bypassing the remaining iterations of the inner loop. This feature is especially useful when you want to efficiently manage control flow in complex nested loops.

Example 6:

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

Advanced Examples on Continue Statement


Example 7: Skipping specific values in a loop

<?php
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

foreach ($numbers as $number) {
   if ($number % 2 == 0) {
      continue; // Skip even numbers
   }
   echo $number . " ";
}
?>
Output:
       1 3 5 7 9 11

Example 8: Filtering data based on conditions

<?php
$students = [
     ["name" => "Alice", "grade" => 85],
     ["name" => "Bob", "grade" => 60],
     ["name" => "Charlie", "grade" => 90],
     ["name" => "Dave", "grade" => 50],
];

foreach ($students as $student) {
    if ($student["grade"] < 65) {
        continue; // Skip students with grades below 65.
    }
    echo $student["name"] . " passed with grade " . $student["grade"] . "\n";
}
?>
Output:
      Alice passed with grade 85
      Charlie passed with grade 90

In this tutorial, you have learned about the continue statement in PHP which is a powerful control flow statement to control loop behavior. It allows you to skip specific iterations based on specific conditions, making code cleaner and more efficient.