Parsing PHP jump out of the loop and the difference between continue, break, exit _php tutorial

Source: Internet
Author: User
The loop structure in PHP has roughly a for loop, while loop, do{} while loop and several foreach loops, regardless of the loop, in There are several ways to jump out of a loop in PHP:
Code:
Copy CodeThe code is as follows:
$i = 1;
while (true) {//It looks like this loop is going to run all the time.
if ($i ==2) {//2 skip not displayed
$i + +;
Continue
} else if ($i ==5) {//But here $i=5 jumps out of the loop.
Break
} else {
Echo $i. '
';
}
$i + +;
}
exit;
Echo ' here does not output ';
?>

Results:
1
3
4
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:
Copy CodeThe code is as follows:
for ($i = 1; $i <=; $i + +) {
if ($i% 3 = = 0 | | $i% 7 = = 0) {
Continue
}
& #160; else{
echo "$i \ n
”;
}
}
?>

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:
Copy CodeThe code is as follows:
for ($i = 1; $i <=; $i + +) {
for ($j = 1; $j <=; $j + +) {
$m = $i * $i + $j * $J;
echo "$m \ n
”;
if ($m < | | $m > 190) {
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
Copy CodeThe code is as follows:
for ($i = $; $i >= 1; $i –) {
if (sqrt ($i) <= 29) {
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.
Copy CodeThe code is as follows:
for ($i = $; $i >= 1; $i –) {
if (sqrt ($i) >= 29) {
echo "$i \ n
”;
}
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.
Copy CodeThe code is as follows:
for ($i = $; $i >= 1; $i –) {
if (sqrt ($i) >= 29) {
echo "$i \ n
”;
}
else{
Return
}
}
echo "The bank will not be exported";
?>

The example here is the same as the effect of using exit above.
At the end of the loop, nature jumps out
This, of course, is best understood when the loop satisfies the cyclic critical condition by exiting itself.
The above is a simple summary of several ways to jump out of a loop in PHP.

http://www.bkjia.com/PHPjc/327957.html www.bkjia.com true http://www.bkjia.com/PHPjc/327957.html techarticle 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 roughly a few ways to jump out of a loop in PHP: Code ...

  • 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.