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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:00:52  来源:igfitidea点击:

What is meant by a number after "break" or "continue" in PHP?

phploopsbreakpointsbreakcontinue

提问by kn3l

Could someone please explain, with examples, what is meant by loop break 2or continue 2in PHP? What does it mean when breakor continueis followed by a number?

有人可以用例子解释一下循环break 2continue 2PHP中的含义吗?当breakcontinue后面跟着一个数字是什么意思?

回答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 13because 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 ABbecause 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, continuestops the current iteration execution but lets another to run, while breakstops the whole statement completely.
So we can ell that continueis applicable for the loops only, whereas breakcan 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, breakin 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 2in 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;
    }
}
?>

More examples of 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";
}
?>

More examples of continue

更多继续的例子

回答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 $arrayvalue 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';
}

}

}