PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

php中break的作用

angryTom
angryTom 原创
2019-08-23 13:54:56 3003浏览

break 在一些计算机编程语言中是保留字,其作用大多情况下是终止所在层的循环。在 C语言 的 switch(开关语句)中,break 语句还可用来在执行完一个 case(分支)后立即跳出当前 switch 结构。在某些程序调试过程中则使用break设置断点。下面我们就介绍一下break在PHP中的作用。

推荐教程:PHP视频教程

break 结束当前 for,foreach,while,do-while 或者 switch 结构的执行。

break 可以接受一个可选的数字参数来决定跳出几重循环。

<?php
    $arr = array(&#39;one&#39;, &#39;two&#39;, &#39;three&#39;, &#39;four&#39;, &#39;stop&#39;, &#39;five&#39;);
    while (list (, $val) = each($arr)) {
        if ($val == &#39;stop&#39;) {
            break; /* You could also write &#39;break 1;&#39; 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;
            }
        }
?>
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。