[英]Difference between break and continue in PHP?
break
完全结束一个循环, continue
只是当前迭代的快捷方式并继续下一次迭代。
while ($foo) { <--------------------┐
continue; --- goes back here --┘
break; ----- jumps here ----┐
} |
<--------------------┘
这将像这样使用:
while ($droid = searchDroids()) {
if ($droid != $theDroidYoureLookingFor) {
continue; // ..the search with the next droid
}
$foundDroidYoureLookingFor = true;
break; // ..off the search
}
break 退出您所在的循环, continue 立即开始循环的下一个循环。
例子:
$i = 10;
while (--$i)
{
if ($i == 8)
{
continue;
}
if ($i == 5)
{
break;
}
echo $i . "\n";
}
将输出:
9
7
6
作为记录:
请注意,在 PHP 中,出于continue的目的, switch语句被视为循环结构。
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>";
}
希望能帮到你;
'continue' 在循环结构中用于跳过当前循环迭代的其余部分,并在条件评估和下一次迭代开始时继续执行。
'break' 结束当前 for、foreach、while、do-while 或 switch 结构的执行。
break 接受一个可选的数字参数,它告诉它要打破多少嵌套的封闭结构。
查看以下链接:
http://www.php.net/manual/en/control-structures.break.php
http://www.php.net/manual/en/control-structures.continue.php
希望能帮助到你..
Break 结束当前循环/控制结构并跳到它的末尾,不管循环会重复多少次。
继续跳到循环的下一次迭代的开始。
break
将停止当前循环(或传递一个整数来告诉它要中断多少个循环)。
continue
将停止当前迭代并开始下一个迭代。
break
将退出循环,而continue
将立即开始循环的下一个循环。
我在这里没有写任何相同的东西。 只是 PHP 手册中的变更日志说明。
更改日志继续
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.
休息的更新日志
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.