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 have an if statement if($number == 4)
what would the line of code be after that that would skip anything after that line and start the loop at 5? break, return, exit?
Use the JavaScript continue statement to skip the current iteration of a loop and continue the next one.
The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.
Use the continue statement inside the loop to skip to the next iteration in Python. However, you can say it will skip the current iteration, not the next one.
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With