Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How php specifies to jump out of several layers of loops

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to specify php to jump out of several layers of cycles". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to specify php to jump out of several layers of cycles" can help you solve the problem.

In php, you can use the breakstatement to specify how many layers of loops to jump out of, which allows the program to jump out of not only the current loop, but also the specified number of layers; syntax "break n;", where the parameter "n" specifies the number of layers of loops to jump out.

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

In php, you can use the break statement to specify several layers of loops to jump out.

Php break statement

The break keyword allows the program to jump out of the current loop and can be used in statements such as switch, for, while, and do while, which terminates the code in the body of the loop and immediately jumps out of the current loop and executes the code after the loop.

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

Example: declare an infinite loop using while, constantly generating random numbers within 1 to 20 in the loop, and when the random number equals 10:00, use break to exit the loop

The running results are as follows:

12, 13, 9, 14, 19, 12, 17, 8, 10, $num = 10, exit the loop! Successfully exit the while loop!

The break statement can not only jump out of the current loop, but also specify several layers of loop to jump out. The syntax format is:

Break n

Where parameter n specifies the number of loop layers to jump out, as shown in the following figure:

Example: using a 3-layer loop, the outermost while loop and the middle layer for loop are infinite loops, and the innermost two for loops are juxtaposed: the program first executes the first for loop, and when the variable $I equals 3, it jumps out of the current cycle (the first loop) and continues to execute the second for loop. When the variable system in the second for cycle equals 4, it will jump out of the outermost loop directly.

The running results are as follows:

The variable $I is equal to 3, which jumps out of the first cycle. The variable $j equals 4, which jumps out of the outermost cycle. This is the end of the introduction to "how php specifies to jump out of several layers of loops". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report