PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

switch> <break
Last updated: Fri, 16 May 2008

view this page in

continue

L'instruction continue est utilisée dans une boucle afin d'éluder les instructions de l'itération courante et de continuer l'exécution à la condition de l'évaluation et donc, de commencer la prochaine itération.

Note: Notez qu'en PHP, la structure switch est considérée comme une boucle par continue.

continue accepte un argument numérique optionnel qui vous indiquera combien de structures emboîtées ont été ignorées.

Exemple #1 Instruction continue

<?php
while (list($cle$valeur) = each($arr)) {
    if (!(
$cle 2)) { // évite les membres impairs
        
continue;
    }
    
fonction_quelconque($valeur);
}

$i 0;
while (
$i++ < 5) {
    echo 
"Dehors<br />\n";
    while (
1) {
        echo 
"&nbsp;&nbsp;Milieu<br />\n";
        while (
1) {
            echo 
"&nbsp;&nbsp;Intérieur<br />\n";
            continue 
3;
        }
        echo 
"Ceci n'est jamais atteint.<br />\n";
    }
    echo 
"Ceci non plus.<br />\n";
}
?>

Oublier le point virgule après continue peut porter à confusion. Voici un exemple de ce que vous ne devez pas faire :

Exemple #2 N'oubliez pas le point-virgule après continue

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

On peut s'attendre à ce que le résultat soit :

0
1
3
4

mais ce script affichera :

2

car la valeur de retour de l'appel à print() est int(1), et cela se comportera alors comme si on avait fournit l'argument optionnel mentionné plus haut.



switch> <break
Last updated: Fri, 16 May 2008
 
add a note add a note User Contributed Notes
continue
Geekman
27-Dec-2007 05:01
For clarification, here are some examples of continue used in a while/do-while loop, showing that it has no effect on the conditional evaluation element.

<?php
// Outputs "1 ".
$i = 0;
while (
$i == 0) {
   
$i++;
    echo
"$i ";
    if (
$i == 1) continue;
}

// Outputs "1 2 ".
$i = 0;
do {
   
$i++;
    echo
"$i ";
    if (
$i == 2) continue;
} while (
$i == 1);
?>

Both code snippets would behave exactly the same without continue.
tufan dot oezduman at gmail dot com
21-Dec-2006 04:28
a possible explanation for the behavior of continue in included scripts mentioned by greg and dedlfix above may be the following line of the "return" documentation: "If the current script file was include()ed or require()ed, then control is passed back to the calling file."
The example of greg produces an error since page2.php does not contain any loop-operations.

So the only way to give the control back to the loop-operation  in page1.php would be a return.
szrrya at yahoo dot com
17-Jul-2006 02:18
Documentation states:

"continue is used within looping structures to skip the rest of the current loop iteration"

Current functionality treats switch structures as looping in regards to continue.  It has the same effect as break.

The following code is an example:

<?php
for ($i1 = 0; $i1 < 2; $i1++) {
 
// Loop 1.
 
for ($i2 = 0; $i2 < 2; $i2++) {
   
// Loop 2.
   
switch ($i2 % 2) {
      case
0:
        continue;
        break;
    }
    print
'[' . $i2 . ']<br>';
  }
  print
$i1 . '<br>';
}
?>

This outputs the following:
[0]
[1]
0
[0]
[1]
1

Switch is documented as a block of if...elseif... statements, so you might expect the following output:
[1]
0
[1]
1

This output requires you to either change the switch to an if or use the numerical argument and treat the switch as one loop.
Rene
18-Feb-2006 12:24
(only) the reason that is given on the "Continue with missing semikolon" example is wrong.

the script will output "2" because the missing semikolon causes that the "print"-call is executed only if the "if" statement is true. It has nothing to to with "what" the "print"-call would return or not return, but the returning value can cause to skip to the end of higher level Loops if any call is used that will return a bigger number than 1.

<?php
continue print "$i\n";
?>

because of the optional argument, the script will not run into a "unexpected T_PRINT" error. It will not run into an error, too, if the call after continue does return anything but a number.

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

to
because the print() call will look like the optional numeric argument mentioned above.
net_navard at yahoo dot com
25-Dec-2005 09:01
Hello firends

It is said in manually:
continue also accepts an optional numeric argument which tells it how many levels of enclosing loops it should .

In order to understand better this,An example for that:
<?php

/*continue also accepts an optional numeric argument which
    tells it how many levels of enclosing loops it should skip.*/

for($k=0;$k<2;$k++)
{
//First loop

   
   
for($j=0;$j<2;$j++)
    {
//Second loop

     
for($i=0;$i<4;$i++)
      {
//Third loop
   
if($i>2)
    continue
2;// If $i >2 ,Then it skips to the Second loop(level 2),And starts the next step,
   
echo "$i\n";
       }

    }

}

?>

Merry's christmas :)
   
With regards,Hossein
dedlfix gives me a hint
28-Jan-2005 06: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 08: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 08: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.

switch> <break
Last updated: Fri, 16 May 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites