Analysis of PHP out of the loop method and continue break exit differences

  • 2020-06-23 00:07:16
  • OfStack

In PHP, there are generally for cycle, while cycle, do{} while cycle and foreach cycle. No matter which cycle, there are generally several ways to break out of the cycle in PHP:
Code:

<?php
$i = 1;
while (true) { //  It looks like this cycle right here is going to 1 Direct execution 
    if ($i==2) {// 2 Skip no display 
        $i++;
        continue;
    } else if ($i==5) {//  But here $i=5 You're out of the loop 
        break;
    } else {
        echo $i . '<br>';
    }
    $i++;
}
exit;
echo ' There's no output here ';
?>

Results:
1
3
4
continue
continue is used in the loop structure, where the control program abandons the code after the continue statement in this loop and moves on to the next loop. continue itself does not break out of the loop structure, it just abandons the loop. If you use continue in an acyclic structure (for example, if statement, switch statement), the program will fail.
For example, in the PHP snippet below:

<?php
for($i = 1;$i <= 100; $i++ ){
if($i % 3 == 0 || $i % 7 == 0){
continue;
}
& #160;  else{
echo " $i \n<br/> " ;
}
}
?>

The code snippet of PHP is used to output the natural Numbers within 100 that are not divisible by 7 or 3. In the loop, if condition is used to determine the Numbers that are divisible, and then continue is executed. Statement, and you go straight to the next loop. The following output statement will not be executed.

break
break is used in the various loops and switch statements mentioned above. His job is to jump out of the current syntax and execute the following statement. The break statement can take 1 parameter n, which means 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 layers out of the loop.
Consider this example of multiple loop nesting:

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;
}
}
}

Using break 2 to get out of the loop, you can experiment with 1 eye, remove 2, and get a completely different result. If you don't use parameters, only the loop is out, and the layer 1 loop continues.

goto
goto is really just an operator, and like any other language, PHP discourages abuse of goto, which can lead to serious readability. The purpose of goto is to jump the execution of the program from the current position to any other position. goto itself does not have the function of a loop to end, but its jump position is such that it can be used as an escape loop. However, es58EN 5.3 and above have stopped supporting goto, so goto should be avoided whenever possible.
The following example USES goto to break out of the loop

for($i = 1000;$i >= 1 ; $i �  ){
if( sqrt($i) <= 29){
goto a;
}
echo  " $i " ;
}
a:
echo "  this is the end " ;

goto is used in this example to break out of the loop. This example is used to detect that the square root of those Numbers up to 1000 is greater than 29.

exit
exit is used to end program execution. It can be used anywhere. It doesn't have the meaning of breaking out of the loop. exit can take 1 argument, if the argument is a string, PHP will output the string directly, if the argument is integer plastic (range 0-254), that argument will be used as the end state.

<?php
for($i = 1000;$i >= 1 ; $i �  ){
if( sqrt($i) >= 29){
echo " $i \n<br/> " ;
}
else{
exit;
}
}
echo "The bank will not be exported" ;
?>

In the above example, the code is terminated directly from the loop, which results in the subsequent code not being executed, and even the html code after exit is not printed if it is on an php web page.

return
The return statement is used to end 1 piece of code and return 1 argument. Can be from a function call, from 1 include () or require () statement contains file to call, also can be called in the main program, if it is running at the end of the function in the calling program will immediately and return the parameters, if it is include () or require () statement contains files is called, program execution will be immediately returned to the calling program the file, the return value will be include () or require () returns a value. If it is called in the main program, the main program will immediately stop execution

<?php
for($i = 1000;$i >= 1 ; $i �  ){
if( sqrt($i) >= 29){
echo " $i \n<br/> " ;
}
else{
return;
}
}
echo "The bank will not be exported" ;
?>

The example here is the same as using exit above.
At the end of the loop condition, jump out naturally
And this is of course the best way to think about it, when the loop satisfies the critical condition of the loop it exits itself.
That's a quick summary of some of the ways you can get out of the loop in PHP.

Related articles: