3

I am aware we can skip the next iteration with continue in a for loop. Anyway to skip the next x loops (2 or more)?

giorgio79
  • 3,787
  • 9
  • 53
  • 85

4 Answers4

6

You actually can't, you can do a dirty trick like

for ($i=0; $i<99; $i++){
    if(someCondition) {
        $i = $i + N; // This will sum N+1 because of the $i++ in the for iterator (that fire when the new loop starts)
        continue;
    }
}
Gumma Mocciaro
  • 1,205
  • 10
  • 24
1

If you're iterating with a for loop (as opposed to a foreach loop) you could do something like this:

for ($i=0; $i<$numLoops; $i++) {
    if(condition()) {
        $i+= $numLoopsToSkip;
        continue;
    }
}
Michael Ambrose
  • 992
  • 6
  • 11
1

Take for example, you can define the amount of times you want to loop as you want as $y

<?php
y = 5;

while (true) {
    // do something
    if (y > 0) {
        y--;
        continue;
    }
    // do something else
}
?>
0

Coming soon in PHP 'X' ;-)

continue += x;
Anthony Rutledge
  • 6,980
  • 2
  • 39
  • 44