search for in the  
<breakswitch>
Last updated: Thu, 19 May 2005

continue

continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the beginning of the next iteration.

Note: Note that in PHP the switch statement is considered a looping structure for the purposes of continue.

continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of.

<?php
while (list($key, $value) = each($arr)) {
   if (!(
$key % 2)) { // skip odd members
      
continue;
   }
  
do_something_odd($value);
}

$i = 0;
while (
$i++ < 5) {
   echo
"Outer<br />\n";
   while (
1) {
       echo
"&nbsp;&nbsp;Middle<br />\n";
       while (
1) {
           echo
"&nbsp;&nbsp;Inner<br />\n";
           continue
3;
       }
       echo
"This never gets output.<br />\n";
   }
   echo
"Neither does this.<br />\n";
}
?>

Omitting the semicolon after continue can lead to confusion. Here's an example of what you shouldn't do.

<?php
 
for ($i = 0; $i < 5; ++$i) {
     if (
$i == 2)
         continue
     print
"$i\n";
  }
?>

One can expect the result to be :

0
1
3
4

but this script will output :

2

because the return value of the print() call is int(1), and it will look like the optional numeric argument mentioned above.



User Contributed Notes
continue
njones at fredesign dot company
05-May-2005 01:26
I noticed that continue would not work within a switch statement too, for example :

<?php

$names
= array("Mike", "Mary", "Jose", "Anna");
foreach (
$names as $kid) {
switch(
$kid) {
case
"Mike":
case
"Anna": continue; break;
}
echo
$kid."<br />";
}
?>

This will NOT work as expected. It will produce:

Mike
Mary
Jose
Anna

BUT, if you specify the level of brackets to jump out of for the  continue it will work as expected. So you should have the following code:

<?php
$names
= array("Mike", "Mary", "Jose", "Anna");
foreach (
$names as $kid) {
switch(
$kid) {
case
"Mike":
case
"Anna": continue 2; break;
}
echo
$kid."<br />";
}
?>

This WILL work as expected: It will produce:

Mary
Jose

Luck,
-Nika
spirit
02-May-2005 11:29
continue seems not to work with switch !
this exemple :
<?php
$i
= 0;
switch(
$i) {
case
1 : echo 1 . '<br />'; continue;
case
2 : echo 2 . '<br />'; continue;
default :
$i++; continue;
}
echo
$i;
?>

return 1, the switch is called only once !
Lord Duran
21-Apr-2005 02:55
if (!($key % 2)) { // skip odd members
       continue;
   }

Note that this skips even members, not odd as written. Just a sidenote :) (Of course, this might be because numbering starts at 0, but essentially it'll skip over 0, 2, 4....)
dedlfix gives me a hint
28-Jan-2005 08:47
a possible solution for
greg AT laundrymat.tv

I've got the same problem as Greg
and now it works very fine by using
return() instead of continue.

It seems, that you have to use return()
if you have a file included and
you want to continue with the next loop
greg AT laundrymat.tv
14-Jan-2005 10:58
You using continue in a file included in a loop will produce an error.  For example:

//page1.php
for($x=0;$x<10;$x++)
   {
   include('page2.php');   
}

//page2.php

if($x==5)
   continue;
else
   print $x;

it should print

"012346789" no five, but it produces an error:

Cannot break/continue 1 level in etc.
www.derosetechnologies.com
10-May-2004 10:58
In the same way that one can append a number to the end of a break statement to indicate the "loop" level upon which one wishes to 'break' , one can append a number to the end of a 'continue' statement to acheive the same goal. Here's a quick example:

<?
  
for ($i = 0;$i<3;$i++) {
       echo
"Start Of I loop\n";
       for (
$j=0;;$j++) {
          
           if (
$j >= 2) continue 2; // This "continue" applies to the "$i" loop
          
echo "I : $i J : $j"."\n";
       }
       echo
"End\n";
   }
?>

The output here is:
Start Of I loop
I : 0 J : 0
I : 0 J : 1
Start Of I loop
I : 1 J : 0
I : 1 J : 1
Start Of I loop
I : 2 J : 0
I : 2 J : 1

For more information, see the php manual's entry for the 'break' statement.
CelloG at phpdoc dot org
01-May-2003 08:55
continue works for:

do
while
switch
for
foreach

<breakswitch>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT