Answers for "for loop continue and break php"

PHP
2

for loop php continue to next item

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

foreach($stack as $v){
    if($v == 'second') {
      continue;
    }
    echo $v.'<br>';
}
/*
first
third
fourth
fifth
*/
Posted by: Guest on December-27-2020
1

Difference between break and continue in PHP?

break ends a loop completely, continue just shortcuts the current iteration and moves on to the next iteration.

while ($foo) {   <--------------------┐
    continue;    --- goes back here --┘
    break;       ----- jumps here ----┐
}                                     |
                 <--------------------┘
  
This would be used like so:

while ($droid = searchDroids()) {
    if ($droid != $theDroidYoureLookingFor) {
        continue; // ..the search with the next droid
    }

    $foundDroidYoureLookingFor = true;
    break; // ..off the search
}
Posted by: Guest on October-07-2021

Browse Popular Code Answers by Language