Explain the method of PHP out of circulation and the difference of continue, break and exit

Source: Internet
Author: User
Tags exit continue goto php code require square root

The loop structure in PHP has roughly the for loop, while loop, do{} while loop, and the Foreach Loop, no matter which loop, there are several ways to jump out of the loop in PHP:
Code:

Copy Code code as follows:


<?php


$i = 1;


while (true) {//This looks like this loop will always execute


if ($i ==2) {//2 skipped
not shown

$i + +;


continue;


} else if ($i ==5) {//But here $i=5 jumped out of the loop


break;


} else {


Echo $i. ' <br> ';


}


$i + +;


}


exit;
Echo ' here does not output ';


?>


Results:
1
3
4
continue
Continue is used in a loop structure where the control program discards the code after this circular continue statement and turns to the next loop. Continue itself does not jump out of the loop structure, just abandon this one cycle. If you use continue in a non-circular structure (such as in an if statement, in a switch statement), the program will get an error.
For example, in the following snippet of PHP code:

Copy Code code as follows:


<?php


for ($i = 1; $i <= $i + +) {


if ($i% 3 = 0 | | $i% 7 = 0) {


continue;


}


& #160; else{


echo "$i n<br/>";


}


}


?>


The function of PHP code fragment is output 100, can not be divided by 7 and cannot be divisible by 3 of those natural numbers, in the loop first use if conditional statement to judge those can be divisible by the number, and then execute continue statement, directly into the next loop. The following output statement is not executed.

Break
The break is used in the various loops and switch statements mentioned above. His role is to jump out of the current grammatical structure and execute the following statement. The break statement can take an argument n, indicating the number of layers out of the loop, if you want to jump out of multiple loops, you can use N to indicate the number of jumps out of the layer, if not with the parameter default is out of this cycle.
Look at the following example of multiple loops nesting:

Copy Code code as follows:


for ($i = 1; $i <= $i + +) {


for ($j = 1; $j <= $j + +) {


$m = $i * $i + $j * $J;


echo "$m n<br/>";


if ($m < | | $m > 190) {


break 2;


}


}


}


Here use the break 2 jump out of the double loop, you can test a glance, the 2 removed, the result is completely different. If you do not use parameters, the only jump out of this cycle, the first layer of the loop will continue.

Goto
Goto is actually just an operator, as in other languages, PHP does not encourage misuse of Goto, and misuse of Goto can lead to a serious decline in the readability of the program. Goto is the function of the program to jump from the current position to any other position, Goto itself does not want to end the function of the loop, but its jump position to make it can be used as a jump out of the loop. However, PHP5.3 and above have stopped the support of Goto, so you should try to avoid using Goto.
The following is an example of using Goto jump loop

Copy Code code as follows:


for ($i = 1000 $i >= 1; $i –) {


if (sqrt ($i) <= 29) {


Goto A;


}


echo "$i";


}


A:


echo "This is the end";


The example uses Goto to jump out of the loop, which is used to detect that the square root of those numbers is greater than 29 within 1000.

Exit
exit is used to end a program execution. It can be used anywhere and it has no meaning to jump out of circulation. Exit can take a parameter, if the argument is a string, PHP will directly output the string, if the parameter is integer shaping (range is 0-254), that parameter will be used as an end state.

Copy Code code as follows:


<?php


for ($i = 1000 $i >= 1; $i –) {


if (sqrt ($i) >= 29) {


echo "$i n<br/>";


}


else{


exit;


}


}


echo "The bank will not be output";


?>


The above example simply ends the operation of the code from the loop, which leads to the subsequent code not being executed, if it is in a PHP Web page, and even the HTML code behind the exit will not be exported.

Return
The return statement is used to end a section of code and returns a parameter. Can be invoked from a function, or from a file included in an include () or require () statement, or it can be invoked in the main program, and if it is in a function, the calling program will end immediately and return the argument, if include () or require () Statement, the execution of the program is immediately returned to the program that invoked the file, and the return value will be the return value of include () or require (). And if it's called in the main program, then the main program will stop executing immediately.

Copy Code code as follows:


<?php


for ($i = 1000 $i >= 1; $i –) {


if (sqrt ($i) >= 29) {


echo "$i n<br/>";


}


else{


return;


}


}


echo "The bank will not be output";


?>


The example here is the same as the effect of using exit above.
at the end of the loop, naturally jump out
This is, of course, best understood, when the loop satisfies the cyclic critical condition is to exit itself.
The above is a simple summary of several ways to jump 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.