PHP是否可以跳过两次或多次迭代


PHP Is it possible to skip two or more iterations?

我知道我们可以在for循环中使用continue跳过下一次迭代。无论如何,跳过接下来的x个循环(2个或更多)?

你实际上不能,你可以做这样的肮脏把戏

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;
    }
}

如果使用for循环(而不是foreach循环)进行迭代,则可以执行以下操作:

for ($i=0; $i<$numLoops; $i++) {
    if(condition()) {
        $i+= $numLoopsToSkip;
        continue;
    }
}

例如,您可以将想要循环的次数定义为$y

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

即将在PHP'X'中推出;-)

continue += x;