PHP continue statement

August 3, 2010

PHP “continue” statement is used within the loops to skip the current loop iteration and goes to the next iteration.

See the syntax structure of “continue statement”

while ( expression 1)
{
if ( expression 2 )
{
continue ;
}
// Operation Statements
}

In this structure if expression 2 return TRUE , continue statement will execute and it will skip that iteration and wont execute the rest portions of code after the “continue” command, control will go to the next iteration in while loop with expression 1.

See the sample PHP program with “continue” statement

<?php
$a = 0;
while ( $a < 3)
{
$a++;
$b = 0;
while ( $b < 5)
{
 $b++;
 if ($b==3)
 {
 continue;
 }
 echo $a*$b." ";
}
echo "<br>";
}
?>

Output of this program will be
1 2 4 5
2 4 8 10
3 6 12 15

We can see that it skipped the third records in each loop.

We can use optional parameter with continue statement for specifying how many levels of loops ( not iteration ) should be skipped.

See the following continue syntax structure

while ( expression 1)
{
while ( expression 2 )
{
if ( expression 3 )
{
continue 2;
}
// Operation Statements
}
}

In this structure , if expression 3 becomes TRUE , continue statement will execute with parameter 2. So it will skip the current iteration in while loop with expression 2 also with current iteration with first while loop with expression 1. Next iteration will start from the first while loop.

See the sample php program with continue statement with parameter

<?php
$a = 0;
while ( $a < 3)
{
$a++;
$b = 0;
while ( $b < 5)
{
 $b++;
 if ($b==3)
 {
 continue 2;
 }
 echo $a*$b." ";
}
echo "<br>";
}
?>

Output of this program will be
1 2 2 4 3 6

Here we can see that when $b == 3 , control goes to the first while loop with next iteration. Due to the skipping of rest codes line break statement ( echo “<br>”; ) also wont execute.

Note : Difference between Continue and break statement is that

  • Break statement skip the current loop and control will come out from the current loop structure
  • Continue statement skip the current iteration only, control will goes to the next loop iteration

Do a check the programs with break statement in PHP

Related Tutorials


Break in PHP
“break” statement is using for end the current execution of loop control structure. Its applicable for for, foreach, while, do-while...
elseif statement in PHP
“elseif” statement is used to execute several blocks of if statements. If we want to execute a different statement if...
if statement in PHP
“if” statement is one of the most important and most used construct in PHP programming. Its syntax will look like...
else statement in PHP
“else” statement is an extension with “if” statement in PHP programming. If need to execute a block of statements when...
for in PHP loop programming
for statement is also used to make looping in PHP programming. Its structure is bit more complex than the other...
PHP do while loop
“do while” loop is almost similar to “while” loop in PHP. The expression with “do while” statement will be executed...
PHP Switch case
PHP Switch case statement is an alternative for complex IF statements with a same expression. Its a best alternative for...
while loop in PHP
while statement is using for making a loop in PHP. The while loop will execute as long as the expression...

Tags: ,

One Response to PHP continue statement

  1. Priya on August 3, 2010 at 1:06 pm

    I was looking for the difference between continue and break. Now got the answer. Really good explanations with examples.

Leave a Reply