"break 2"是什么意思?

fun*_*guy 21 php control-structure break

我总是使用和看到只有"休息"的例子.这是什么意思:

 <?php 
    while ($flavor = "chocolate") { 
      switch ($flavor) { 
        case "strawberry"; 
            echo "Strawberry is stock!"; 
            break 2;    // Exits the switch and the while 
        case "vanilla"; 
            echo "Vanilla is in stock!"; 
            break 2;   // Exits the switch and the while 
        case "chocolate"; 
            echo "Chocolate is in stock!"; 
            break 2;    // Exits the switch and the while 
        default;     
            echo "Sorry $flavor is not in stock"; 
            break 2;    // Exits the switch and the while 
      } 
    } 
    ?>
Run Code Online (Sandbox Code Playgroud)

"break"语句是否有更多可用选项?

Jas*_*ary 22

来自PHP文档break:

break接受一个可选的数字参数,告诉它有多少嵌套的封闭结构被打破.

正如评论中所指出的那样,它会突破开关.

以下示例将突破所有foreach循环:

foreach (...) {
  foreach (..) {
    foreach (...) {
      if ($condition) {
        break 3;
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 所以在这种情况下,它打破了开关和时间 (3认同)