php PHP中“中断”或“继续”后的数字是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5167561/
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
What is meant by a number after "break" or "continue" in PHP?
提问by kn3l
Could someone please explain, with examples, what is meant by loop break 2
or continue 2
in PHP? What does it mean when break
or continue
is followed by a number?
有人可以用例子解释一下循环break 2
或continue 2
PHP中的含义吗?当break
或continue
后面跟着一个数字是什么意思?
回答by Your Common Sense
$array = array(1,2,3);
foreach ($array as $item){
if ($item == 2) {
break;
}
echo $item;
}
outputs "1" because the loop was brokenforever, before echo was able to print "2".
输出“1”,因为在 echo 能够打印“2”之前,循环被永远破坏了。
$array = array(1,2,3);
foreach ($array as $item){
if ($item == 2) {
continue;
}
echo $item;
}
outputs 13
because the second iteration was passed
输出,13
因为第二次迭代通过
$numbers = array(1,2,3);
$letters = array("A","B","C");
foreach ($numbers as $num){
foreach ($letters as $char){
if ($char == "C") {
break 2; // if this was break, o/p will be AB1AB2AB3
}
echo $char;
}
echo $num;
}
outputs AB
because of break 2
, which means that both statements was broken quite early. If this was just break
, the output would have been AB1AB2AB3
.
输出AB
因为break 2
,这意味着这两个语句很早就被破坏了。如果只是这样break
,输出就会是AB1AB2AB3
.
$numbers = array(1,2,3);
$letters = array("A","B","C");
foreach ($numbers as $num){
foreach ($letters as $char){
if ($char == "C") {
continue 2;
}
echo $char;
}
echo $num;
}
will output ABABAB
, because of continue 2
: the outer loop will be passed every time.
将输出ABABAB
,因为continue 2
:每次都会通过外循环。
In other words, continue
stops the current iteration execution but lets another to run, while break
stops the whole statement completely.
So we can ell that continue
is applicable for the loops only, whereas break
can be used in other statements, such as switch
.
换句话说,continue
停止当前迭代执行但让另一个运行,同时break
完全停止整个语句。
所以我们可以说它continue
仅适用于循环,而break
可以用于其他语句,例如switch
.
A number represents the number of nestedstatements affected.
if there are 2 nested loops, break
in the inner one will break inner one (however it makes very little sense as the inner loop will be launched again in the next iteration of the outer loop). break 2
in the inner loop will break both.
一个数字表示受影响的嵌套语句的数量。
如果有 2 个嵌套循环,则break
在内部循环中会破坏内部循环(但是它没有多大意义,因为在外循环的下一次迭代中将再次启动内循环)。break 2
在内循环中将破坏两者。
回答by edorian
The number just says "how many scopes to jump out of"
数字只是说“要跳出多少个范围”
<?php
for($i = 0; $i < 10; ++$i) {
for($j = 0; $j < 10; ++$j) {
break 2;
}
}
$i and $j will be 0
$i 和 $j 将为 0
To quote the manual:
引用手册:
continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of.
continue 接受一个可选的数字参数,它告诉它应该跳到结束的封闭循环的级别。
same goes for break.
休息也是一样。
回答by Somnath Muluk
breakaccepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.
break接受一个可选的数字参数,它告诉它要打破多少嵌套的封闭结构。
<?php
$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
while (list(, $val) = each($arr)) {
if ($val == 'stop') {
break; /* You could also write 'break 1;' here. */
}
echo "$val<br />\n";
}
/* Using the optional argument. */
$i = 0;
while (++$i) {
switch ($i) {
case 5:
echo "At 5<br />\n";
break 1; /* Exit only the switch. */
case 10:
echo "At 10; quitting<br />\n";
break 2; /* Exit the switch and the while. */
default:
break;
}
}
?>
continueaccepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. The default value is 1, thus skipping to the end of the current loop.
continue接受一个可选的数字参数,它告诉它应该跳到结束的封闭循环的级别。默认值为 1,因此跳到当前循环的末尾。
<?php
while (list($key, $value) = each($arr)) {
if (!($key % 2)) { // skip odd members
continue;
}
do_something_odd($value);
}
$i = 0;
while ($i++ < 5) {
echo "Outer<br />\n";
while (1) {
echo "Middle<br />\n";
while (1) {
echo "Inner<br />\n";
continue 3;
}
echo "This never gets output.<br />\n";
}
echo "Neither does this.<br />\n";
}
?>
回答by Shakti Singh
break : break the inner most loop (exit from the loop)
break : 中断最里面的循环(退出循环)
break 2 : break the 2 nesting level loops (exit from the 2 nested loops)
break 2 :中断 2 个嵌套级循环(退出 2 个嵌套循环)
continue : force loop for next iteration from where it is used without executing rest of loop code
continue :在不执行其余循环代码的情况下,从使用它的位置强制循环进行下一次迭代
continue 2: force loop for next 2 iteration from where it is used without executing rest of loop code
continue 2:在不执行其余循环代码的情况下从使用它的位置强制循环进行下 2 次迭代
exit the loop when we encounter $array
value to be 5
当我们遇到$array
值为 5时退出循环
break
$array(4,5,8);
for ($i=0 ;$i < 10 $i ++)
{
if ($array[$i]==5)
{
break;
}
}
break (n)
中断 (n)
Exit both loops when we encounter value 5 in $array;
当我们在 $array 中遇到值 5 时退出两个循环;
for ($i=0 ;$i < 10 $i ++)
{
for($j=0; $j <10; $j++)
{
if ($array[$i][$j]==5)
{
break 2;
}
}
}
continue
继续
Will print the message when value is 5;
当值为 5 时将打印消息;
for($i=0; $i<10; $i++)
{
if ($array[$i] != 5)
{
continue;// will reach at the first line from here which is for($i=0;.....
}
echo 'This is five';
}
}
}