skip current iteration skip current iteration php php

skip current iteration


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}


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.)


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:

1246 <- this won't happen