Continue

Last updated on

The PHP continue statement is an expression used to ignore or skip the current code block and move to the next loop. So when PHP sees “continue“, it misses out the rest of the loop steps for that time and starts again from the top.

However, this expression does not exit the loop; it only restarts it for the following field according to the iteration index.

The expression appears and works inside all PHP loops. And it can be written like the below.

continue;

Moreover, the PHP continue statement works with all loops in PHP, such as foreachfor loopwhile loop, and do while loop.

Let’s see how it works.

How the PHP Continue Statement Works

In PHP, when you’re looping through items in a list (or any set of data), continue tells PHP to stop the current loop iteration early if certain conditions are met and immediately start the next iteration. It’s like saying, “If this item doesn’t match what I’m looking for, skip the rest of the steps and check the next item.”.

php continue statement

Let’s consider a list of numbers, and we want to print only the even numbers:

for ($i = 1; $i <= 10; $i++) {
    if ($i % 2 !== 0) {
        continue; // Skip odd numbers
    }
    // This line only runs for even numbers
    echo $i . " is even.\n";
}

In this example, the loop iterates over numbers from 1 to 10. Specifically, the if statement evaluates whether a number is odd (using $i % 2 !== 0 as the condition). Should this condition be met, the ‘continue’ command is executed. Consequently, upon executing ‘continue,’ PHP promptly halts the processing of the current loop iteration and leaps to the beginning of the loop to proceed with the next number.

Anyway, In the next section, you will see more examples.

Examples

Use the PHP continue with for loop.

In this example, the for loop will skip the fourth iteration, meaning it will ignore index number 3. Thus, the output will be as follows:

Loop Index : 0
Loop Index : 1
Loop Index : 2
Loop Index : 4

Additionally, when using the PHP ‘continue’ within a ‘foreach’ loop, you can apply it in the same manner. However, here, I will check the array key to skip the current item.

$arr = array(
     "name" => "Montasser",
     "site" => "CodedTag",
     "age"  => 55
 );
 
 foreach( $arr as $key => $value ){
 
   if ( $key == 'age' ) {
     continue;
   }
   
   echo $value;
   echo "\n"; // new line
 }

This loop will ignore the “age” field of the array. Thus, the result will be as follows:

Montasser
CodedTag 

Let’s summarize it.

Wrapping Up

In this tutorial, you learned how to use the ‘continue’ keyword inside loops and how it functions. Let’s summarize it in a few points.

  • The continue statement in PHP is really like hitting the skip button when you’re going through a list but only want to pay attention to certain things.
  • This command does not terminate the loop; it simply initiates it anew for the subsequent item based on the iteration index.

Thank you for reading. Happy Coding!

Share on: