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中,switch语句被视为循环结构,因此适用于continue。
continue 2
。 - flaviovsbreak语句用于跳出循环,而continue语句则是在满足特定条件时停止当前脚本执行,并继续循环直到结束。
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
会停止当前循环(或传递一个整数告诉它要从多少个循环中退出)。
continue
会停止当前迭代并开始下一个迭代。
Break语句可以结束当前的循环/控制结构,并跳到它的结尾,无论这个循环本来应该重复多少次。
而Continue语句则是跳过当前循环的剩余部分,立即进入下一次迭代。
break
会退出循环,而continue
会立即开始循环的下一个周期。
我这里没有写任何东西。这只是PHP手册中的变更日志。
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.
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.
switch
语句中的break
和continue
作用相同,都会退出switch
语句。如果要退出外部循环(例如for
循环),可以使用continue 2
。 - Amin.Qarabaqi