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 key_exists() Function

PHP key_exists() Function The key_exists() function in PHP checks if the specified key or index exists in the array or not. Syntax key_exists ( mixed $key , array $array )  Parameter key(required)- This parameter represents the value to check. array(required)- This parameter signifies an input array with...

1 minute read.

Composer Installation

Composer is an application-level package manager for the PHP Programming language. The development began in April 2011 and it was first released on March 1, 2012. Commands: require install update remove Installation:  Download the installer by visiting...

1 minute read.

PHP ksort() Function

PHP ksort() Function The ksort() function in PHP sorts an array by key while maintaining the keys. Syntax ksort(array & $array[,int $sort_flags = SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This parameter is...

2 minutes read.

PHP array_merge_recursive() Function

PHP array_merge_recursive() Function The array_merge_recursive() function  in PHP merges the elements of one or arrays together and returns the resulting array. Syntax array_merge_recursive (array $array  , [ array $... ] ) Parameter array(required)- This parameter specifies the input array ….(optional)- It represents more arrays to...

1 minute read.

PHP For Loops

PHP for loop is used when the specified condition is predefined. Syntax: for (initialization; condition; increment/decrement ) { code to be executed; } Example <!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php echo " Print counting using for loop"."<br>"; for($i=1;$i<=5;$i++){ echo $i."<br>"; } ?> </body> </html> Output Print counting using...

1 minute read.

PHP is_int() Function

The PHP is_int() 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...

1 minute read.

PHP serialize() Function

PHP serialize() Function The serialize() function in PHP is used to convert a storable representation of a value. Syntax serialize ( mixed $value ) Parameter value(required)- This parameter represents the value to be serialized. Return This function returns a string containing a byte-stream representation...

1 minute read.

Python if-else

Python if-else The else statement is associated with if statement. An else statement executes if the conditional expression in if the statement becomes false or a Zero value. If the conditional expression...

2 minutes read.

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.

PHP strnatcasecmp() Function

PHP strnatcasecmp() Function The strnatcasecmp() function in PHP compares two strings using a "natural" algorithm. It is a case-insensitive function. Syntax strnatcasecmp ( 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...

1 minute read.

PHP substr_count() Function

PHP substr_count() Function The substr_count() function in PHP counts the number of times a substring occurs in the specified string. Syntax substr_count ( string $haystack , string $needle [, int $offset  [, int $length ]] ) Parameter haystack(required)- This parameter signifies the string to search in. needle (required)- This parameter represents the substring to search...

1 minute read.

PHP compact() function

PHP compact() function The compact() function in PHP is used to create an array containing variables and their values. It returns the output array whose keys are the variable names, and their corresponding values are...

1 minute read.

PHP str_word_count() Function

PHP str_word_count() Function The str_word_count() function in PHP counts the number of words inside the string and returns the information about words used in a string. String str_word_count( string $string [, int $format [, string $charlist ]] ) Parameter string(required)- This parameter represents the input string. format(required)- This parameterspecifies the...

2 minutes read.

PHP in_array() Function

PHP in_array() Function The in_array() function in PHP checks if the value exists in the specified array or not. Syntax in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )  Parameter needle(required)- This parameter represents the searched value. haystack(required)- This parameter represents the input array. strict(optional)- This parameter...

1 minute read.

PHP setlocale() Function

PHP setlocale() Function The setlocale() function in PHP sets locale information(language, monetary, time and, other information specific for a geographical area.) Syntax setlocale ( int $category , array $locale ) Parameter category(required)- This parameter specified a named constant specifying the category of the functions affected...

1 minute read.

PHP array_unshift() Function

PHP array_unshift() Function The array_unshift () function in PHP prepends one or more elements to the beginning or front of an array. It returns the new number of elements in the array. Syntax array_unshift ( array &$array [, mixed $... ] ) Parameter array(required)- This array represents...

1 minute read.

PHP htmlspecialchars() Function

PHP htmlspecialchars() Function The htmlspecialchars() function in PHP converts some special predefined characters to HTML entities. The special predefined characters are as follows: & (ampersand) OR &amp;" (double quote) OR &quot;' (single quote) OR &#039;<...

2 minutes read.

PHP addslashes() Function

PHP addslashes() Function The addslashed() function in PHP is used to return a string with backslashes in front of predefined characters. This function is only used with some characters which are...

1 minute read.

PHP Magic Constants

There are various predefine PHP Magic Constants. It starts with underscore(__) and ends with double underscore.  Magic Constant Description __LINE__ To check current line number of the file. __FILE__ To check full path and filename of...

1 minute read.

PHP substr_compare() Function

PHP substr_compare() Function The substr_compare() function in PHP compares two strings from a specified start position. It is a binary-safe and optionally case-sensitive function. Syntax substr_compare ( string $main_str , string $str , int $offset [, int $length [, bool$case_insensitivity]] )  Parameter main_str(required)- This parameter specifies the first string to compare. str(required)- This parameter...

1 minute read.