your image

PHP Continue - javatpoint

javapoint
Related Topic
:- PHP Java computer programming

PHP continue statement

The PHP continue statement is used to continue the loop. It continues the current flow of the program and skips the remaining code at the specified condition.

The continue statement is used within looping and switch control structure when you immediately jump to the next iteration.

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

Syntax

The syntax for the continue statement is given below:

 

427.7K

PHP Design Patterns course preview - PHP Dependency injection and factory pattern -Advanced OOP PHP

  1. jump-statement;  
  2. continue;  

Flowchart:

PHP Continue Example with for loop

Example

In the following example, we will print only those values of i and j that are same and skip others.

  1. <?php  
  2.     //outer loop  
  3.     for ($i =1; $i<=3; $i++) {  
  4.         //inner loop  
  5.         for ($j=1; $j<=3; $j++) {  
  6.             if (!($i == $j) ) {  
  7.                 continue;       //skip when i and j does not have same values  
  8.             }  
  9.             echo $i.$j;  
  10.             echo "</br>";  
  11.         }  
  12.     }  
  13. ?>  

Output:

112233

PHP continue Example in while loop

Example

In the following example, we will print the even numbers between 1 to 20.

  1. <?php  
  2.     //php program to demonstrate the use of continue statement  
  3.   
  4.     echo "Even numbers between 1 to 20: </br>";  
  5.     $i = 1;  
  6.     while ($i<=20) {  
  7.         if ($i %2 == 1) {  
  8.             $i++;  
  9.             continue;   //here it will skip rest of statements  
  10.         }  
  11.         echo $i;  
  12.         echo "</br>";  
  13.         $i++;  
  14.     }     
  15. ?>  

Output:

Even numbers between 1 to 20:2468101214161820

PHP continue Example with array of string

Example

The following example prints the value of array elements except those for which the specified condition is true and continue statement is used.

 

  1. <?php  
  2.     $number = array ("One", "Two", "Three", "Stop", "Four");  
  3.     foreach ($number as $element) {  
  4.         if ($element == "Stop") {  
  5.             continue;  
  6.         }  
  7.         echo "$element </br>";  
  8.     }  
  9. ?>  

Output:

OneTwoThreeFour

PHP continue Example with optional argument

The continue statement accepts an optional numeric value, which is used accordingly. The numeric value describes how many nested structures it will exit.

Example

Look at the below example to understand it better:

  1. <?php  
  2.     //outer loop  
  3.     for ($i =1; $i<=3; $i++) {  
  4.         //inner loop  
  5.         for ($j=1; $j<=3; $j++) {  
  6.             if (($i == $j) ) {      //skip when i and j have same values  
  7.                 continue 1;     //exit only from inner for loop   
  8.             }  
  9.             echo $i.$j;  
  10.             echo "</br>";  
  11.         }  
  12.     }     
  13. ?>  

Output:

 

121321233132

Comments