This article provides a detailed analysis of PHP's method of jumping out of the loop and the differences between continue, break, and exit. for more information, see the for loop structure in PHP, while loop, do {} while loop, and foreach loop.
PHP has the following methods:
Code:
The code is as follows:
$ I = 1;
While (true) {// It seems that this loop will be executed all the time
If ($ I = 2) {// 2 skip not show
$ I ++;
Continue;
} Else if ($ I = 5) {// But Here $ I = 5 jumps out of the cycle.
Break;
} Else {
Echo $ I .'
';
}
$ I ++;
}
Exit;
Echo 'do not output here ';
?>
Result:
1
3
4
Continue
Continue is used in the loop structure. the control program abandons the code after this loop continue statement and proceeds to the next loop. The continue itself does not jump out of the loop structure, but just gives up this loop. If you use continue in a non-cyclic structure (for example, in the if statement, switch statement), the program will fail.
For example, in the following PHP code snippet:
The code is as follows:
For ($ I = 1; $ I <= 100; $ I ++ ){
If ($ I % 3 = 0 | $ I % 7 = 0 ){
Continue;
}
& #160; else {
Echo "$ I \ n
";
}
}
?>
The function of PHP code snippets is to output less than 100 of the natural numbers that cannot be divisible by 7 and cannot be divisible by 3. in a loop, use the if condition statement to determine the number that can be divisible, then execute the continue statement to directly enter the next loop. The following output statement is not executed.
Break
Break is used in various loops and switch statements mentioned above. Its role is to jump out of the current syntax structure and execute the following statements. The break statement can contain a parameter n, indicating the number of layers that exit the loop. to jump out of multiple loops, you can use n to represent the number of layers that exit, if the parameter is not included, the loop jumps out.
The following is an example of multi-loop nesting:
The code is as follows:
For ($ I = 1; $ I <= 10; $ I ++ ){
For ($ j = 1; $ j <= 10; $ j ++ ){
$ M = $ I * $ I + $ j * $ j;
Echo "$ m \ n
";
If ($ m <90 | $ m> 190 ){
Break 2;
}
}
}
Here, break 2 is used to jump out of the dual loop. you can take a test and remove 2. the results are completely different. If you do not use the parameters, only this loop exists, and the first loop continues to run.
Goto
Goto is actually just an operator. Like other languages, PHP does not encourage abuse of goto. misuse of goto will cause serious reduction in program readability. The role of goto is to jump the execution of the program from the current position to any other position. the goto itself does not have the function of stopping the loop, however, its jump position can be used as a jump out loop. However, PHP5.3 and later versions do not support goto, so avoid using goto as much as possible.
The following is an example of using the goto jump out of the loop.
The code is as follows:
For ($ I = 1000; $ I >=1; $ I -){
If (sqrt ($ I) <= 29 ){
Goto;
}
Echo "$ I ";
}
A:
Echo "this is the end ";
In this example, goto is used to jump out of the loop. In this example, the square root of the numbers within 1000 is greater than 29.
Exit
Exit is used to end program execution. It can be used anywhere and has no meaning of jumping out of the loop. Exit can contain a parameter. if the parameter is a string, PHP will output the string directly. if the parameter is an integer (the range is 0-254 ), the parameter will be used as the end state.
The code is as follows:
For ($ I = 1000; $ I >=1; $ I -){
If (sqrt ($ I) >=29 ){
Echo "$ I \ n
";
}
Else {
Exit;
}
}
Echo "this line will not be output ";
?>
In the above example, code execution is directly completed from the loop, which will prevent subsequent code execution. if it is on a php web page, even html code after exit is not output.
Return
The return statement ends a piece of code and returns a parameter. It can be called from a function, or from a file contained in an include () or require () statement, or in the main program, if the program is called in a function, it will immediately end running and return parameters. if the file contained in the include () or require () statement is called, program execution will immediately return the program that calls the file, and the returned value will be the return value of include () or require. If it is called in the main program, the main program will immediately stop running
The code is as follows:
For ($ I = 1000; $ I >=1; $ I -){
If (sqrt ($ I) >=29 ){
Echo "$ I \ n
";
}
Else {
Return;
}
}
Echo "this line will not be output ";
?>
The example here is the same as the example above with exit.
Automatically jumps out at the end of the loop.
Of course, it is better to understand this. when the cycle meets the critical conditions of the cycle, it is to exit by yourself.
The above is a summary of several methods for jumping out of the loop in PHP.