The loop structure in PHP has roughly a for loop, while loop, do{} while loop, and several foreach loops, regardless of the loop, there are several ways to jump out of a loop in PHP:
Code:
<?php$i = 1;while (True) {//Here it appears that this loop will always be executed if ($i ==2) {//2 skip does not show $i + +; Continue; } else if ($i ==5) {//But here $i=5 jumps out of the loop break ; } else { echo $i. "<br>"; } $i + +;} Exit;echo "not output here";? >
Results:
134
Continue
Continue is used in the loop structure, and the control program discards the code after the loop continue statement and turns to the next loop. Continue itself does not jump out of the loop structure, just give up this cycle. If you use continue in a non-circular structure, such as in a switch statement, the program will have an error.
For example, in the following snippet of PHP code:
<?phpfor ($i = 1; $i <=, $i + +) {if ($i% 3 = = 0 | | $i 7 = = 0) {continue;} Else{echo "$i \n<br/>";}}? >
PHP's code fragment is output within 100, can not be divisible by 7 and not divisible by 3 of those natural numbers, the first in the loop with the IF condition statement to determine those can be divisible by the number, and then execute continue; statement, directly into the next loop. The following output statements are not executed.
Break
Break is used in the various loops and switch statements mentioned above. His role is to jump out of the current syntax structure and execute the following statement. The break statement can take a parameter n, which indicates the number of layers that jump out of the loop, and if you want to jump out of multiple loops, you can use N to indicate the number of layers that jumped out, or if the default is to jump out of the heavy loop without parameters.
Look at the following example of a multiple loop nesting:
for ($i = 1; $i <=, $i + +) {for ($j = 1; $j <=; $j + +) {$m = $i * $i + $j * $J; echo "$m \n<br/>"; if ($m < 90 | | $m >) {break 2;}}}
Here the break 2 is used to jump out of the double loop, you can test one eye, the 2 is removed, the result is completely different. If you do not use parameters, just jump out of this cycle, the first layer of the loop will continue to execute.
Goto
Goto is actually just an operator, and as with other languages, the misuse of Goto is discouraged in PHP, and the misuse of Goto can lead to a severe decrease in the readability of the program. The role of Goto is to jump the execution of a program from its current position to any other position, and the Goto itself does not have a loop to end, but its jump position makes it possible to use it as a jump out loop. However, the PHP5.3 and above versions stop support for Goto, so you should avoid using Goto as much as possible.
The following is an example of using a goto jump loop
for ($i = $i >= 1; $i –) {if (sqrt ($i) <=) {goto A;} echo "$i";} A:echo "This is the end";
In the example, Goto is used to jump out of the loop, which is used to detect 1000 or less, and the square root of those numbers is greater than 29.
Exit
Exit is used to end the execution of the program. Can be used anywhere, and itself does not jump out of the meaning of the loop. Exit can take a parameter, if the argument is a string, PHP will directly output the string, if the argument is integer (the range is 0-254), that parameter will be used as the end state.
<?phpfor ($i = $; $i >= 1; $i-) {if (sqrt ($i) >=) {echo "$i \n<br/>";} Else{exit;}} echo "The bank will not be exported";? >
The above example directly in the loop to end the operation of the code, which will cause the subsequent code will not be executed, if it is in a PHP Web page, even after the exit of the HTML code will not be output.
Return
The return statement is used to end a piece of code and return a parameter. It can be called from a function, or it can be called from a file contained in an include () or require () statement, or it can be called in the main program, and if it is called in a function, the program will end immediately and return the argument, if it is an include () or require () Statement contains a file that is called, the execution of the program is returned immediately to the program that called the file, and the return value will be the return value of the include () or require (). If it is called in the main program, the main program will stop executing immediately.
<?phpfor ($i = $; $i >= 1; $i-) {if (sqrt ($i) >=) {echo "$i \n<br/>";} Else{return;}} echo "The bank will not be exported";? >
The example here has the same effect as using exit above.
at the end of the loop, naturally jumping out
This is of course the best understanding, when the loop to meet the critical conditions of the cycle is the exit itself.
The above is a simple summary of several ways to jump out of a loop in PHP.