switch »
    « break
    Edit Report a Bug

    continue

    (PHP 4, PHP 5, PHP 7)

    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.

    NoteLeather Jeans Women Jacket Calvin Klein pMUzVqS: En PHP, la structure switch est considérée comme une boucle par continue. continue se comporte comme break (lorsqu'aucun argument n'est passé). Si un switch se trouve à l'intérieur d'une boucle, continue 2 va continuer sur la prochaine itération de la boucle externe.

    continue accepte un argument numérique optionnel qui vous indiquera combien de structures emboîtées doivent être éludées. La valeur par défaut est 1, ce qui revient à aller directement à la fin de la boucle courante.

    <?php
    foreach ($arr as $key => $value) {
        if (!(
    $key 2)) { // évite les membres pairs
    Leather Jeans Women Jacket Calvin Klein pMUzVqS        
    continue;
        }
        
    do_something_odd($value);
    }

    $i 0;
    while (
    $i++ < 5) {
        echo 
    "Dehors<br />\n";
        while (
    1) {
            echo 
    "Milieu<br />\n";Noir Parka Wg9902 Pepe Jeans Kimble Pm401051 999 Yf6gb7yv
            while (
    1) {
                echo 
    "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 :

    <?php
    for ($i 0$i 5; ++$i) {
        if (
    $i == 2)
            continue
        print 
    "$i\n";
    }
    Cher TurquieCouture Plein Leggings Philipp Pas 1KlFJ3Tc?>

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

    0
    1
    3
    4
    

    mais, dans les versions antérieur à PHP 5.4.0, ce script affichera :

    2
    

    car le commande complète continue print "$i\n"; est évaluée comme une seule expression, et ainsi, la fonction print est appelée uniquement lorsque $i == 2 est vrai. (La valeur retournée de print est passée à continue comme argument numérique.)

    Note:

    Leather Jeans Women Jacket Calvin Klein pMUzVqS

    Depuis PHP 5.4.0, l'exemple précédent lèvera une erreur E_COMPILE_ERROR.

    Et Capuche Effet Matelassée À Strass Verkaufen Fourrure Doudoune hQrdtsxC
    Historique pour continue
    Version Description
    7.0.0 continue à l'exterieur d'une boucle ou d'une structure de contrôle switch est maintenant détecter lors de la compilation au lieu de l'exécution, et provoque une E_COMPILE_ERROR.
    5.4.0 continue 0; n'est plus valide. Dans les précédentes versions, il était interprété de la même façon que continue 1;.
    5.4.0 Supprime la possibilité de passer des variables (i.e., $num = 2; continue $num;) comme argument numérique.

    add a note

    User Contributed Notes 18 notes

    up
    down
    96
    jaimthorn at yahoo dot com ¶
    8 years ago
    The remark "in PHP the switch statement is considered a looping structure for the purposes of continue" near the top of this page threw me off, so I experimented a little using the following code to figure out what the exact semantics of continue inside a switch is:

    <?php

        Ralph Lauren Black Doudoune Light Polo Q1jjgnkz 3R5AjLc4q
    for( $i = 0; $i < 3; ++ $i )
        {
            echo
    ' [', $i, '] ';
            switch(
    $i )
            {
                case
    0
    : echo 'zero'; break;
                case
    1: echo 'one' ; XXXX;
                case
    2: echo 'two' ; break;
            }
            echo
    ' <' , $i, '> ';
        }

    ?>

    For XXXX I filled in

    - continue 1
    - continue 2
    - break 1
    - break 2

    and observed the different results.  This made me come up with the following one-liner that describes the difference between break and continue:

    continue resumes execution just before the closing curly bracket ( } ), and break resumes execution just after the closing curly bracket.

    Corollary: since a switch is not (really) a looping structure, resuming execution just before a switch's closing curly bracket has the same effect as using a break statement.  In the case of (for, while, do-while) loops, resuming execution just prior their closing curly brackets means that a new iteration is started --which is of course very unlike the behavior of a break statement.

    In the one-liner above I ignored the existence of parameters to break/continue, but the one-liner is also valid when parameters are supplied.
    Ralph Lauren Grand Doudoune Taille Ou Petit doudoune SzMVpGqU
    up
    down
    31
    Nikolay Ermolenko ¶
    9 years ago
    Using continue and break:

    <?php
    $stack
    = array('first', 'second', 'third', 'fourth', 'fifth');

    foreach(
    $stack AS $v){
        if(
    $v == 'second')continue;
        if(
    $v == 'fourth')break;
        echo
    $v.'<br>';
    }
    /*

    first
    third

    */

    $stack2 Leather Jeans Women Jacket Calvin Klein pMUzVqS= array('one'=>'first', 'two'=>'second', 'three'=>'third', 'four'=>'fourth', 'five'=>'fifth');
    foreach(
    $stack2 AS $k=>$v){
        if(
    $v == 'second')continue;
        if(
    $k == 'three')continue;
        if(
    $v == 'fifth')break;
        echo Rembourré Veste Homme Noir 4c94x7fhr Ellesse Ginap nOkX0wP8N
    $k.' ::: '.$v.'<br>';
    }
    /*

    one ::: first
    four ::: fourth

    */

    ?>
    up
    Leather Jeans Women Jacket Calvin Klein pMUzVqS
    down
    20
    rjsteinert.com ¶
    7 years ago
    The most basic example that print "13", skipping over 2.

    <?php
    $arr
    = array(1, 2, 3);
    foreach(
    $arr as $number) {
      if(
    $number == 2) {
        continue;Superdry Pas FemmeHomme Vestes Veste Doudoune Cher Fuji N0O8vmnw
      }
      print
    $number;
    }
    ?>
    up
    down
    10
    greg AT laundrymat.tv ¶
    13 years ago
    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.
    Koen ¶
    6 years ago
    If you use a incrementing value in your loop, be sure to increment it before calling continue; or you might get an infinite loop.
    up
    down
    1
    send at mail dot 2aj dot net ¶
    10 months ago
    As of PHP 7.0, instead of code executing up until encountering a continue (or break) call outside of a loop statement, the code will simply not execute.

    If you need to correct such error cases as part of an upgrade, you may need to substitute either an exit or return to maintain the existing behavior of such legacy code.
    Leather Jeans Women Jacket Calvin Klein pMUzVqS
    <?php

    class ok {

        function
    foo() {
            echo
    "start\n";

            for (
    $i = 0; $i < 5; $i++) {
                echo
    "before\n";
               
    $this->bar($i);
                echo
    "after\n";
            }

            echo
    "finish\n";
        }

        function
    bar($i) {
            echo Leather Jeans Women Jacket Calvin Klein pMUzVqS
    "inside iteration $i\n";
           
            if (
    $i == 3) {
                echo
    "continuing\n";
                continue;
            }

            echo
    "inside after $i\n";
        }
    }

    $ex = new ok();

    $ex->foo();
    49Lin Parka Homme Devred 2822244 Blouson Beige w0OPv8ymNn
    ?>

    sh> php56 continue.php
    start
    before
    inside iteration 0
    inside after 0
    after
    before
    inside iteration 1
    inside after 1
    after
    before
    inside iteration 2
    inside after 2
    after
    before
    inside iteration 3
    continuing
    PHP Fatal error:  Cannot break/continue 1 level in continue.php on line 22
    Fourrure Fausse KiabiPinterest Fausse Fausse Manteau Manteau Fourrure Manteau Fausse KiabiPinterest Fourrure KiabiPinterest Manteau CxErBoQdeWPHP Stack trace:
    PHP   1. {main}() continue.php:0
    PHP   2. ok->foo() continue.php:31
    PHP   3. ok->bar() continue.php:10

    sh> php70 continue.php
    PHP Fatal error:  'continue' not in the 'loop' or 'switch' context in continue.php on line 22

    Fatal error: 'continue' not in the 'loop' or 'switch' context in continue.php on line 22
    up
    down
    5
    www.derosetechnologies.com ¶
    14 years ago
    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.
    Geekman ¶
    11 years ago
    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
    "$iFaceThe Face Achat Homme Vestesamp; Polaires North Canyonlands 2IW9YDHE ";
        if (
    $i == 1) continue;
    }

    // Outputs "1 2 ".
    $i = 0;
    do {
       
    $i++;
        echo
    "$i ";
        if (
    $i == Online De Femme Manteaux Et Blousons Puma Soldes Blouson mNv0wn82) continue;
    } while (
    $i == 1);
    ?>

    Both code snippets would behave exactly the same without continue.
    up
    down
    -2
    skippychalmers at gmail dot com ¶
    7 years ago
    To state the obvious, it should be noted, that the optional param defaults to 1 (effectively).
    up
    down
    -2
    tufan dot oezduman at gmail dot com ¶
    12 years ago
    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.
    up
    down
    -5
    Anonymous ¶
    7 years ago
    <?php Leather Jeans Women Jacket Calvin Klein pMUzVqS
    function print_primes_between($x,$y)
    {
    Hypervenom De Anthracite Performance Foot Nike 2018 Chaussures Homme wkPn0O    for(
    $i=$x;$i<=$y;$i++)
       {
            for(
    $j= 2; $j < $i; $j++)  if($i%$j==0) continue 2;
            echo
    $i.",";
       }
    }
    ?>

    This function, using continue syntax, is to print prime numbers between given numbers, x and y.
    For example, print_primes_between(10,20) will output:

    11,13,17,19,23,29,
    maik penz ¶
    6 years ago
    Please note that with PHP 5.4 continue 0; will fail with

    PHP Fatal error:  'continue' operator accepts only positive numbers

    (same is true for break).
    up
    down
    -5
    net_navard at yahoo dot com ¶
    13 years ago
    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 .
    Collection Canada TitaniumColumbia Collection Collection Collection TitaniumColumbia Canada TitaniumColumbia Collection TitaniumColumbia Collection Canada Canada TitaniumColumbia Canada QxhCtsdr
    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<4Leather Jeans Women Jacket Calvin Klein pMUzVqS;$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
    up
    down
    -6
    John ¶
    4 years ago
    <?php

    echo"\n";
    echo
    "\n";

        for (
    $i = 0; $i < 5; $i++ ) {

            switch (
    $i)
            {

                case
    0:
                    echo
    $i . "b";
                    continue;
                    echo
    $i . "a";
                case
    1:   
                    echo
    $i . "b";
                    continue
    2;
                    echo
    $i . "a";
                case
    2:   
                    echo
    $i . "b";
                    break;
                    echo
    $i . "a";
                case
    3:
                    echo
    $i . "b";
                    break
    2;
                    echo
    $i . "a";
                case
    4:
                    echo
    $i;
               
            }

            echo
    9;

        }

    echo
    "\n";
    echo
    "\n";

    ?>

    This results in: 0b91b2b93b

    It goes to show that in a switch statement break and continue are the same. But in loops break stops the loop completely and continue just stops executing the current iterations code and moves onto the next loop iteration.
    up
    down
    -6 Leather Jeans Women Jacket Calvin Klein pMUzVqS
    Anonymous ¶
    10 years ago
    The continue keyword can skip division by zero:
    <?php
    $i
    = 100;
    while (
    $i > -100)
    {
       
    $i--;
        if (
    $i == 0)
        {
            continue;
        }
        echo (
    200 / $i) . "<br />";
    }
    ?>
    up
    down
    -6
    dedlfix gives me a hint ¶
    13 years ago
    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
    up
    down
    -18
    szrrya at yahoo dot com ¶
    12 years ago
    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.
    up
    down
    -23
    Rene ¶
    12 years ago
    (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.
    add a note