PHP Continue

What is continue in PHP?

Inside the loop, the continue statement is used to control the loop in preprocessor hypertext. PHP utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the beginning of the loop and skips the current statement once it is reached.

  • Continue is a predefined keyword in PHP.
  • The continue statement can’t be used outside of the iteration loop. It only appears in iteration statements and causes the control to be passed to the starting of that loop in which it is used. To be more specific, each of the statements.
  • The continue statement is used in all php control loops (for, while, and do-while).

Syntax:

The syntax of the continue statement is: 
     Jump statement;
            continue;

Explanation:

The statements within the loop are executed based on the loop condition. The continue statement skips the current statement and passes control to the next loop iteration when used inside a condition.

Flowchart:

PHP Continue

Syntax of for loop:

In PHP, a for loop is written as

for (initialize; condition; increment) 
 {
 statement(x);
 }

Use of continue in for loop:

The continue keyword in a for loop causes control to jump to the update statement right away.

for ( , , ) {         
   {                                           
     // ...            
  continue;
   }
 } 

Syntax of the while loop:

while(condition)
                     {
                       
statement(x);
                     }

Use of Continue in while loop:

  while () {         
 {                                           
     // ...            
 }                                           
 continue;
 } 

Syntax of the Do-While loop:

 Do{
     statement(s);
       }   while(expression);

Use of Continue in a do-while loop:

  do ( ){         
   {                                           
     // ...            
  continue;
updation;
   }
 } while(,) 

Let us take some examples to understand the continue statement in PHP:

Program 1: Program to print numbers:

<?php  
    //outer loop 
    for($i =1; $i<=3; $i++)
               {  
        //inner loop
        for($j=1; $j<=3; $j++) 
                  {  
                     if(($i == $j) )
                           {  
                               continue;   // it skips if both i and j are not equal
                           }  
            echo $i.$j;  
             echo “</br>”;
        }  
    }  
             ?>  

The output of the program:

11
22
33
44
55

Program 2: Program to print odd numbers between 1 and 20 by using the Continue statement:

      echo "Even numbers between 1 to 20: </br>";  
    $i = 1;  
    while($i<=20){
   if($i%2==0){ 
            $i++; 
       Continue;     //it skips all even numbers
        }  
        echo $i;  
        echo”</br>”; 
        $i++;  
               }     
?>  

The output of the program:

1
3 
5 
7
9
11
13
15
17
19

Explanation:

We have used the while loop method in the above code to demonstrate the implementation of the continue statement.

We've set up two variables: "n" for entering the value and "i" for the iteration. The program asks the user for a number, which is then stored in variable ‘n’. We initialized i to 1(i=1) and used a condition as text expression that verifies the value of "i" is less than or equal to 20 (i<=20).

After then, we used the continue statement inside the loop to skip the even numbers with the logic (i%2==0). if the condition is true according to the logic then the number is skipped. And, if the number is not even, the statement outside if condition is executed, and the odd numbers are printed one by one.

Program 3: Program to print the numbers:

<?php
for ($i = 1; $i<=5; $i++)
 {
   echo "outer loop\n";
   for ($j=1;$j<=5;$j++)
 {
      if ($j >3) continue 2;
      echo "I : $i J : $j"."\n";
   }
   echo "End of inner loop\n";
}
?>

The Output of the program:

outer loop
i : 1 J : 1
i : 1 J : 2
i : 1 J : 3
outer loop
i : 2 J : 1
i : 2 J : 2
i : 2 J : 3
outer loop
i : 3 J : 1
i : 3 J : 2
i : 3 J : 3
outer loop
i : 4 J : 1
i : 4 J : 2
i : 4 J : 3
outer loop
i : 5 J : 1
i : 5 J : 2
i : 5 J : 3

Related Topics

PHP is_long() Function

The PHP is_long () function in PHP is used to find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it returns...

1 minute read.

WordPress Theme

Writing our WordPress Theme is relatively simple. Requirements: Before we begin, we need to know about the following set of requirements. We need to have full knowledge of WordPress setup, which is either...

2 minutes read.

PHP stristr() Function

PHP stristr() Function The stristr() function in PHP finds the first occurrence of a string and returns the matched substring. It is a case-insensitive function. Syntax stristr ( string $haystack , mixed $needle [, bool $before_needle ] ) Parameter haystack(required)- This parameter represents the input string. needle(required)- This parameter...

2 minutes read.

PHP str_shuffle() Function

PHP str_shuffle() Function The str_shuffle() function in PHP randomly shuffles all the characters of the specified string. Syntax str_shuffle ( string $str ) Parameter str(required)- This parameter represents the input string to shuffle. Return This function returns the shuffled string. Example 1 ...

1 minute read.

PHP is_double() Function

The PHP is_double() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

PHP is_float() Function

The PHP is_float() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

PHP array_walk() Function

PHP array_walk() Function The array_walk() function in PHP is used to apply the user-defined callback function to each element of the array. This function returns a Boolean TRUE on success or FALSE on failure. Syntax array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] ) Parameter array(required)- This array represents...

1 minute read.

PHP array_shift() Function

PHP array_shift() Function The array_shift() in PHP shifts the first value of the array off. This function returns the same value after shortening the array by one element and moving everything. Syntax array_shift ( array &$array )  Parameter array(required)- This parameter represents the input array Return This function returns...

1 minute read.

PHP is_string() Function

PHP is_string() Function The is_ string() function in PHP finds whether a variable is a string or not. It returns a Boolean value TRUE if the parameter var is string, else it returns FALSE. Syntax is_string ( mixed $var ) Parameter var(required)- This parameter represents...

1 minute read.

PHP convert_uudecode() Function

PHP convert_uudecode() Function The convert_uudecode() function in PHP decodes a uuencoded string. Syntax convert_uudecode ( string $data ) Parameter data- This parameter represents the uuencoded data. Return This function returns the decoded data as a string or FALSE on failure. Example 1 <?php // initializing the string $str...

1 minute read.

PHP stripcslashes() Function

PHP stripcslashes() Function The stripcslashes() function in PHP returns a string with backslashes(\n, \r ..., octal and hexadecimal representation) stripped off. Syntax stripcslashes ( string $str )  Parameter str(required)- This parameter signifies the string to be unescaped. Return This function returns the unescaped string with backslashes stripped off. Example 1   ...

1 minute read.

PHP strval() function

PHP strval() function The strval() function in PHP is used to get the string value for the specified variable. This function cannot be used on arrays or objects, if applied then would return only the...

1 minute read.

PHP array_chunk() Function

PHP array_chunk() Function The array_chunk() function in PHP is used to split an array into chunks where the last chunk may contain less than size elements. Syntax array_chunk ( array $array , int $size [, bool $preserve_keys ]...

1 minute read.

PHP strcmp() Function

PHP strcmp() Function The strcmp() function in PHP compares two strings. It is a binary-safe case-sensitive string function. This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each...

1 minute read.

PHP strnatcmp() Function

PHP strnatcmp() Function The strnatcmp() function in PHP compares two strings using a "natural" algorithm. It is case-sensitive. Syntax strnatcmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than(...

1 minute read.

PHP levenshtein() Function

PHP levenshtein() Function The levenshtein() function in PHP calculates the Levenshtein distance between two strings. Syntax levenshtein ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del )  Parameter str1(required)- This parameter signifies one of the strings being evaluated for Levenshtein distance. str2(required)- This parameter signifies one of the strings being evaluated...

1 minute read.

PHP str_getcsv() Function

PHP str_getcsv() Function The str_getcsv() function in PHP parses a CSV string into an array and returns an array containing the fields read. Syntax str_getcsv ( string $input [, string $delimiter [, string $enclosure [, string$escape ]]] ) Parameter input(required)- This parameter represents the string to parse. delimiter(optional)- This parameter signifies...

1 minute read.

PHP chop() Function

PHP chop() Function The chop() function in PHP removes whitespaces or other predefined characters from the right end of the specified string. Syntax chop(str,charlist) Parameter str(required)- This parameter specifies the string to check. charlist(optional)- This parameter...

1 minute read.

PHP is_object() Function

The is_ object() function in PHP finds whether the specified variable is an object or not. It returns a Boolean value TRUE if the specified var is an object else it returns FALSE. Syntax is_object(mixed$var) var(required)- This parameter represents the...

1 minute read.

PHP array_merge() Function

PHP array_merge() Function The array_merger() Function in PHP merges the elements of one or more arrays wherein the values of one are appended to the end of the previous one. It returns the resulting array...

1 minute read.