What is the difference between break
and continue
in PHP?
在PHP中break和continue的区别是什么?
9 个解决方案
#1
382
break
ends a loop completely, continue
just shortcuts the current iteration and moves on to the next iteration.
完全中断循环,继续当前迭代的快捷方式,继续到下一个迭代。
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
}
#2
32
break exits the loop you are in, continue starts with the next cycle of the loop immediatly.
中断退出循环,继续从循环的下一个循环开始。
Example:
例子:
$i = 10;
while (--$i)
{
if ($i == 8)
{
continue;
}
if ($i == 5)
{
break;
}
echo $i . "\n";
}
will output:
将输出:
9
7
6
#3
12
打破:
break ends execution of the current for, foreach, while, do-while or switch structure.
中断当前对、for、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 here for an artical detailing Break vs Continue with a number of examples
此外,请参阅这里的文章细节突破vs继续与一些例子
#4
9
For the Record:
备案:
Note that in PHP the switch statement is considered a looping structure for the purposes of continue.
注意,在PHP中,出于继续的目的,switch语句被视为循环结构。
#5
4
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.
中断当前循环/控制结构并跳至其末尾,无论循环重复多少次。
Continue skips to the beginning of the next iteration of the loop.
继续跳转到循环的下一个迭代的开始。
#6
4
'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.
“断开”结束对当前、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..
希望它能帮助. .
#7
4
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;
希望它能对你有所帮助;
#8
2
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将停止当前迭代并开始下一个迭代。
#9
2
break
will exit the loop, while continue
will start the next cycle of the loop immediately.
break将退出循环,而continue将立即启动循环的下一个循环。
#1
382
break
ends a loop completely, continue
just shortcuts the current iteration and moves on to the next iteration.
完全中断循环,继续当前迭代的快捷方式,继续到下一个迭代。
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
}
#2
32
break exits the loop you are in, continue starts with the next cycle of the loop immediatly.
中断退出循环,继续从循环的下一个循环开始。
Example:
例子:
$i = 10;
while (--$i)
{
if ($i == 8)
{
continue;
}
if ($i == 5)
{
break;
}
echo $i . "\n";
}
will output:
将输出:
9
7
6
#3
12
打破:
break ends execution of the current for, foreach, while, do-while or switch structure.
中断当前对、for、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 here for an artical detailing Break vs Continue with a number of examples
此外,请参阅这里的文章细节突破vs继续与一些例子
#4
9
For the Record:
备案:
Note that in PHP the switch statement is considered a looping structure for the purposes of continue.
注意,在PHP中,出于继续的目的,switch语句被视为循环结构。
#5
4
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.
中断当前循环/控制结构并跳至其末尾,无论循环重复多少次。
Continue skips to the beginning of the next iteration of the loop.
继续跳转到循环的下一个迭代的开始。
#6
4
'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.
“断开”结束对当前、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..
希望它能帮助. .
#7
4
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;
希望它能对你有所帮助;
#8
2
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将停止当前迭代并开始下一个迭代。
#9
2
break
will exit the loop, while continue
will start the next cycle of the loop immediately.
break将退出循环,而continue将立即启动循环的下一个循环。