php中while能break吗,php中break的作用

7a55832f1f8b772dbfb49d9e91144cd9.png

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

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

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
/n";

}

/* Using the optional argument. */

$i = 0;

while (++$i) {

switch ($i) {

case 5:

echo "At 5
/n";

break 1; /* Exit only the switch. */

case 10:

echo "At 10; quitting
/n";

break 2; /* Exit the switch and the while. */

default:

break;

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值