PHP continue

The PHP continue statement is used to continue the loop if a specified condition occurs.The PHP continue statement executes next iteration in the loop continue.

The PHP continue statement can be used with all types of loops such as  for, while, do-while and foreach loop.  The continue statement allows us to skip the execution of the code inside loop for the given condition.

PHP Syntax:

continue;

PHP continue statement for For Loop

In the below example,  It prints all value except 3.

PHP Code Example 1:

<?php
        for($i=0;$i<=8;$i++) {
          if ($i==3) {
            continue;  // skip execution for value of i is 3
          }
          echo "The number is: $i <br>";
        }

       echo "Welcome to aryatechno! <br>";
?>

Output:

The number is: 0
The number is: 1
The number is: 2
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
Welcome to aryatechno!

PHP continue statement for while Loop

In the below example,  It prints all value except 3.

PHP Code Example 2:

<?php
    $num=1;
    while($num <= 5) {
      if ($num==3) {  

                $num++;
                continue;  //skip execution for value of num variable is 3
      }    
      echo "The number is: $num <br>";
      $num++;
    }
?>

Output:

The number is: 1
The number is: 2
The number is: 4
The number is: 5

PHP continue statement for foreach Loop

In the below example,  It prints all value except white color.

PHP Code Example 3:

<?php  
        $color = array("red", "orange", "white", "blue", "yellow");  
        foreach($color as $key=>$value) {  
            if ($value=="white") {  
                continue;    //skip execution for white color.
            }  
            echo "color is $value </br>";  
        }  
?> 

Output:

color is red
color is orange
color is blue
color is yellow

Comments

Leave a Reply

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

27870