php PHP中break和continue的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4364757/
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
Difference between break and continue in PHP?
回答by deceze
break
ends a loop completely, continue
just shortcuts the current iteration and moves on to the next iteration.
break
完全结束一个循环,continue
只是缩短当前迭代并移动到下一次迭代。
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
}
回答by Hinek
break exits the loop you are in, continue starts with the next cycle of the loop immediatly.
break 退出您所在的循环, continue 立即开始循环的下一个循环。
Example:
例子:
$i = 10;
while (--$i)
{
if ($i == 8)
{
continue;
}
if ($i == 5)
{
break;
}
echo $i . "\n";
}
will output:
将输出:
9
7
6
回答by SW4
break ends execution of the current for, foreach, while, do-while or switch structure.
break 结束当前 for、foreach、while、do-while 或 switch 结构的执行。
continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
continue 在循环结构中用于跳过当前循环迭代的其余部分,并在条件评估和下一次迭代开始时继续执行。
So depending on your need, you can reset the position currently being executed in your code to a different level of the current nesting.
因此,根据您的需要,您可以将代码中当前正在执行的位置重置为当前嵌套的不同级别。
Also, see herefor an artical detailing Break vs Continue with a number of examples
另外,请参阅此处以了解有关 Break 与 Continue 的详细说明以及许多示例
回答by Igor Parra
For the Record:
作为记录:
Note that in PHP the switchstatement is considered a looping structurefor the purposes of continue.
请注意,在 PHP中,出于continue的目的,switch语句被视为循环结构。
回答by Joko Wandiro
break used to get out from the loop statement, but continue just stop script on specific condition and then continue looping statement until reach the end..
break 用于退出循环语句,但继续只是在特定条件下停止脚本,然后继续循环语句直到到达结束..
for($i=0; $i<10; $i++){
if($i == 5){
echo "It reach five<br>";
continue;
}
echo $i . "<br>";
}
echo "<hr>";
for($i=0; $i<10; $i++){
if($i == 5){
echo "It reach end<br>";
break;
}
echo $i . "<br>";
}
Hope it can help u;
希望能帮到你;
回答by DGH
Break ends the current loop/control structure and skips to the end of it, no matter how many more times the loop otherwise would have repeated.
Break 结束当前循环/控制结构并跳到它的末尾,不管循环会重复多少次。
Continue skips to the beginning of the next iteration of the loop.
继续跳到循环的下一次迭代的开始。
回答by Mahendra Liya
'continue' is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
'continue' 在循环结构中用于跳过当前循环迭代的其余部分,并在条件评估和下一次迭代开始时继续执行。
'break' ends execution of the current for, foreach, while, do-while or switch structure.
'break' 结束当前 for、foreach、while、do-while 或 switch 结构的执行。
break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.
break 接受一个可选的数字参数,它告诉它要打破多少嵌套的封闭结构。
Check out the following links:
查看以下链接:
http://www.php.net/manual/en/control-structures.break.php
http://www.php.net/manual/en/control-structures.break.php
http://www.php.net/manual/en/control-structures.continue.php
http://www.php.net/manual/en/control-structures.continue.php
Hope it helps..
希望能帮助到你..
回答by alex
break
will stop the current loop (or pass an integer to tell it how many loops to break from).
break
将停止当前循环(或传递一个整数来告诉它要中断多少个循环)。
continue
will stop the current iteration and start the next one.
continue
将停止当前迭代并开始下一个迭代。
回答by cspolton
break
will exit the loop, while continue
will start the next cycle of the loop immediately.
break
将退出循环,whilecontinue
将立即开始循环的下一个循环。
回答by Muhammed Shihabudeen Labba A
I am not writing anything same here. Just a changelog note from PHP manual.
我在这里没有写任何相同的东西。只是 PHP 手册中的变更日志说明。
Changelog for continue
更改日志继续
Version Description
7.0.0 - continue outside of a loop or switch control structure is now detected at compile-time instead of run-time as before, and triggers an E_COMPILE_ERROR.
5.4.0 continue 0; is no longer valid. In previous versions it was interpreted the same as continue 1;.
5.4.0 Removed the ability to pass in variables (e.g., $num = 2; continue $num;) as the numerical argument.
Changelog for break
休息的更新日志
Version Description
7.0.0 break outside of a loop or switch control structure is now detected at compile-time instead of run-time as before, and triggers an E_COMPILE_ERROR.
5.4.0 break 0; is no longer valid. In previous versions it was interpreted the same as break 1;.
5.4.0 Removed the ability to pass in variables (e.g., $num = 2; break $num;) as the numerical argument.