Home>Article>Backend Development> 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

青灯夜游
青灯夜游 Original
2022-02-16 19:34:55 3280browse

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.

'; break; } } echo '成功退出 while 循环!'; ?>

The running results are as follows:

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

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;

Among them, the parameternspecifies 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.

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

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,跳出最外层循环。

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!

Statement:
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