skip current iteration

前端 未结 4 1169
醉话见心
醉话见心 2020-12-28 11:44

I have a php array $numbers = array(1,2,3,4,5,6,7,8,9)

if I am looping over it using a foreach foreach($numbers as $number)

and hav

相关标签:
4条回答
  • 2020-12-28 12:03

    You are looking for the continue statement. Also useful is break which will exit the loop completely. Both statements work with all variations of loop, ie. for, foreach and while.

    $numbers = array( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
    foreach( $numbers as $number ) {
        if ( $number == 4 ) { continue; }
        // ... snip
    }
    
    0 讨论(0)
  • 2020-12-28 12:11

    Break; will stop the loop and make compiler out side the loop. while continue; will just skip current one and go to next cycle. like:

    $i = 0;
    while ($i++)
    {
        if ($i == 3)
        {
            continue;
        }
        if ($i == 5)
        {
            break;
        }
        echo $i . "\n";
    }
    

    Output:

    1
    2
    4
    6 <- this won't happen
    
    0 讨论(0)
  • 2020-12-28 12:18
    continue;
    

    Continue will tell it to skip the current iteration block, but continue on with the rest of the loop. Works in all scenerios (for, while, etc.)

    0 讨论(0)
  • 2020-12-28 12:19

    I suppose you are looking for continue statement. Have a look at http://php.net/manual/en/control-structures.continue.php

    dinel

    0 讨论(0)
提交回复
热议问题