Home > Backend Development > PHP Tutorial > break and continue in PHP

break and continue in PHP

WBOY
Release: 2016-07-29 08:59:54
Original
1103 people have browsed it

(这应该是这两天学习PHP时感到最有意思的地方了)

/*
 * 在PHP中break语句不仅可以跳出当前循环,还可以指定跳出几层循环
 * break $num;  num为向外跳的层数 num不能大于最大循环层数
 */

//  第三重循环
while(true) {
    //  第二重
    for(;;) {
        //  第一重
        for($i = 0; $i <= 10; $i++) {
            echo "$i ";
            if($i == 7) {
                echo "i=7, 跳出1重循环";
                break;
            }
        }
        echo "\n";
        //  第一重
        for($i = 0; $i <= 20; $i++){
            echo "$i ";
            if($i == 15) {
                echo "i=15, 跳出3重循环";
                break 3;
            }
        }
        echo "绝对不会输出这里";
    }
}

/*
 * PHP中continue只能终止本次循环而进入到下一次循环中,
 * continue $num 可以指定终止第几重的当前循环  num不能大于最大循环层数
 */
$arr = array(1,2,3,4,5,6,7,8,9,10);
for($i = 0; $i < 10; $i++) {
    echo "\n";
    if($i % 2 == 0){
        continue;
    }
    for(;;){
        for($j = 0; $j < count($arr); $j++) {
            if($j == $i){
                continue 3; //终止第三层的当前循环
            }else{
                echo "\$arr[".$j."]:".$arr[$j]." ";
            }
        }
    }
    echo "这里也绝对不会输出";

}
Copy after login

以上就介绍了PHP中的break与continue,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template