Analysis of PHP jump-out methods and the differences between continue, break, and exit

Source: Internet
Author: User

The loop structures in PHP are roughly for loop, while loop, do {} while loop, and foreach loop. PHP has the following methods:
Code:
Copy codeThe Code is as follows: <? Php
$ 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. '<br> ';
}
$ 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:
Copy codeThe Code is as follows: <? Php
For ($ I = 1; $ I <= 100; $ I ++ ){
If ($ I % 3 = 0 | $ I % 7 = 0 ){
Continue;
}
& #160; else {
Echo "$ I \ n <br/> ";
}
}
?>

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:Copy codeThe Code is as follows: for ($ I = 1; $ I <= 10; $ I ++ ){
For ($ j = 1; $ j <= 10; $ j ++ ){
$ M = $ I * $ I + $ j * $ j;
Echo "$ m \ n <br/> ";
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.
Copy codeThe 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.Copy codeThe Code is as follows: <? Php
For ($ I = 1000; $ I >=1; $ I -){
If (sqrt ($ I) >=29 ){
Echo "$ I \ n <br/> ";
}
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 runningCopy codeThe Code is as follows: <? Php
For ($ I = 1000; $ I >=1; $ I -){
If (sqrt ($ I) >=29 ){
Echo "$ I \ n <br/> ";
}
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.