在PHP中,"break"或"continue"后面的数字是什么意思?

56
请问有人能够解释一下在 PHP 中什么是带数字的循环控制语句 break 2 或者 continue 2 吗?可以给出一些例子吗?当 break 或者 continue 后面跟着一个数字时,这代表什么意思呢?请详细说明。
4个回答

89
$array = array(1,2,3);
foreach ($array as $item){
  if ($item == 2) {
    break;
  }
  echo $item;
}

由于循环被永久地打破,所以输出为"1",在echo打印"2"之前。

$array = array(1,2,3);
foreach ($array as $item){
  if ($item == 2) {
    continue;
  }
  echo $item;
}

第二次迭代被跳过,因此输出结果为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;
}

输出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;
}

将输出ABABAB,因为有continue 2:每次都会跳过外部循环。

换句话说,continue停止当前迭代的执行,但让另一个继续运行,而break完全停止整个语句。
所以我们可以认为continue只适用于循环,而break可以用于其他语句,比如switch

数字表示影响的嵌套语句数量。
如果有2个嵌套循环,则在内部循环中使用break将打破内部循环(尽管这没有什么意义,因为内部循环将在外部循环的下一次迭代中再次启动)。在内部循环中使用break 2将同时打破两个循环。


39

这个数字只表示“跳出几层作用域”

<?php
for($i = 0; $i < 10; ++$i) {
    for($j = 0; $j < 10; ++$j) {
        break 2;
    }
}

$i和$j将为0

引用手册:

continue接受一个可选的数字参数,它告诉它应该跳过多少层包围循环以结束。

对于break也是一样。


我更喜欢你的答案,因为它简洁明了。 - n8jadams

10

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;
    }
}
?>

更多的break例子

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

更多关于continue的示例


4

break:跳出最内层的循环(退出循环)

break 2:跳出两层嵌套的循环(退出两层嵌套循环)

continue:强制循环进行下一次迭代,而不执行其余的循环代码

continue 2:强制循环进行下两次迭代,而不执行其余的循环代码

当我们遇到$array值为5时退出循环

 break
    $array(4,5,8);
    for ($i=0 ;$i < 10 $i ++)
    {
        if ($array[$i]==5)
        {
          break;
        }
    }

break (n)

当我们在$array中遇到值为5时,退出两个循环;

for ($i=0 ;$i < 10 $i ++)
  {
    for($j=0; $j <10; $j++)
     {
            if ($array[$i][$j]==5)
            {
              break 2;
            }
     }
 }

继续

当值为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';
}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接