Home > Backend Development > PHP Problem > How to specify how many levels of loops to jump out of in php

How to specify how many levels of loops to jump out of in php

青灯夜游
Release: 2023-03-15 11:16:02
Original
3476 people have browsed it

In PHP, you can use the break statement to specify how many levels of loops to jump out of. This statement can not only make the program jump out of the current loop, but also jump out of the specified number of levels of loops; the syntax "break n;", where Parameter "n" specifies the number of loop levels to jump out of.

How to specify how many levels of loops to jump out of in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In php, you can use break statement to specify how many levels of loops to jump out of.

php break statement

The break keyword can make the program jump out of the current loop, which can be used in switch, for, while and do Used in while and other statements, this can terminate the code of the loop body and immediately jump out of the current loop and execute the code after the loop.

The execution flow of the break keyword is shown in the figure below:

How to specify how many levels of loops to jump out of in php

Example: Use while to declare an infinite loop, in the loop Continuously generate random numbers within 1~20. When the random number is equal to 10, use break to exit the loop.

<?php
    while (true) {
        $num = rand(1, 20);
        echo $num.&#39;, &#39;;
        if ($num == 10) {
            echo &#39;$num = 10,退出循环!<br>&#39;;
            break;
        }
    }
    echo &#39;成功退出 while 循环!&#39;;
?>
Copy after login

The running results are as follows:

12, 13, 9, 14, 19, 12, 17, 8, 10, $num = 10,退出循环!
成功退出 while 循环!
Copy after login

The break statement can not only jump out of the current loop, but also You can specify how many levels of loops to jump out of. The syntax format is:

break n;
Copy after login

Among them, the parameter n specifies the number of loop levels to jump out of, as shown in the following figure:

How to specify how many levels of loops to jump out of in php

Example:Use 3 layers of loops. The outermost while loop and the middle for loop are both infinite loops. The innermost two for loops are parallel: the program first executes the first for loop. When the variable $i When it is equal to 3, it will jump out of the current loop (the first loop) and continue to execute the second for loop. When the variable system in the second for loop is equal to 4, it will jump out of the outermost loop directly.

<?php
    while (true) {
        for (;;) {
            for ($i=0; $i < 10; $i++) {
                echo &#39;$i = &#39;.$i.&#39;, <br>&#39;;
                if($i == 3) {
                    echo &#39;变量 $i 等于 3,跳出第一层循环。<br>&#39;;
                    break 1;
                }
            }
            for ($j=0; $j < 10; $j++) {
                echo &#39;$j = &#39;.$j.&#39;, <br>&#39;;
                if($j == 4) {
                    echo &#39;变量 $j 等于 4,跳出最外层循环。&#39;;
                    break 3;
                }
            }
        }
        echo &#39;由于前面直接跳出了最外层循环,所以这里的打印信息是看不到的!&#39;;
    }
?>
Copy after login

The running results are as follows:

$i = 0,
$i = 1,
$i = 2,
$i = 3,
变量 $i 等于 3,跳出第一层循环。
$j = 0,
$j = 1,
$j = 2,
$j = 3,
$j = 4,
变量 $j 等于 4,跳出最外层循环。
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to specify how many levels of loops to jump out of in php. For more information, please follow other related articles on the PHP Chinese website!

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