php 跳过当前迭代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5023897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
skip current iteration
提问by Hailwood
I have a php array $numbers = array(1,2,3,4,5,6,7,8,9)
我有一个 php 数组 $numbers = array(1,2,3,4,5,6,7,8,9)
if I am looping over it using a foreach foreach($numbers as $number)
如果我使用 foreach 循环遍历它 foreach($numbers as $number)
and have an if statement if($number == 4)
并有一个 if 语句 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?
在那之后的代码行会跳过该行之后的任何内容并从 5 开始循环?休息,返回,退出?
回答by Matthew Scharley
You are looking for the continuestatement. Also useful is breakwhich will exit the loop completely. Both statements work with all variations of loop, ie. for
, foreach
and while
.
您正在寻找continue语句。也有用的break将完全退出循环。这两个语句都适用于循环的所有变体,即。for
,foreach
和while
。
$numbers = array( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
foreach( $numbers as $number ) {
if ( $number == 4 ) { continue; }
// ... snip
}
回答by Brad Christie
continue;
Continue will tell it to skip the current iteration block, but continue on with the rest of the loop. Works in all scenerios (for, while, etc.)
Continue 将告诉它跳过当前的迭代块,但继续循环的其余部分。适用于所有场景(for、while 等)
回答by Jaymin
Break; will stop the loop and make compiler out side the loop. while continue; will just skip current one and go to next cycle. like:
休息; 将停止循环并使编译器在循环之外。同时继续;将跳过当前循环并进入下一个循环。喜欢:
$i = 0;
while ($i++)
{
if ($i == 3)
{
continue;
}
if ($i == 5)
{
break;
}
echo $i . "\n";
}
Output:
输出:
1
2
4
6 <- this won't happen
回答by dinel
I suppose you are looking for continue statement. Have a look at http://php.net/manual/en/control-structures.continue.php
我想你正在寻找 continue 语句。看看http://php.net/manual/en/control-structures.continue.php
dinel
迪内尔