PHP 是否可以跳过两次或更多次迭代?
PHP Is it possible to skip two or more iterations?
我知道我们可以在 for 循环中使用 continue 跳过下一次迭代。无论如何要跳过下一个 x 循环(2 个或更多)?
如果您使用 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
}
?>
你实际上不能,你可以做一些卑鄙的把戏,比如
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;
}
}
即将在 PHP 'X' 中推出 ;-)
continue += x;
我知道我们可以在 for 循环中使用 continue 跳过下一次迭代。无论如何要跳过下一个 x 循环(2 个或更多)?
如果您使用 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
}
?>
你实际上不能,你可以做一些卑鄙的把戏,比如
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;
}
}
即将在 PHP 'X' 中推出 ;-)
continue += x;