Home > Backend Development > PHP Tutorial > Examples of usage of PHP loop statements to control break and continue

Examples of usage of PHP loop statements to control break and continue

WBOY
Release: 2016-07-25 08:57:57
Original
1500 people have browsed it
This article introduces the specific usage of break out of the loop and skip a certain level of loop continue in PHP. Friends in need can refer to it.

This article will guide you through the use of break and continue and their role in loop statements.

1, break (int); statement //The function of break is to exit the loop body. When a number is added after break, it indicates which level of loop to exit. Mostly used in while, do...while, for, foreach, switch statements.

Example:

<?php
/**
* break 退出循环
* edit by bbs.it-home.org
*/

$i = 1;
while(true){
echo “PHP视频教程”.$i;
$i++;
if($i==10){
break;
}
}//当循环语句的$i数字自增到10时就执行break语句,直接跳出了while的循环体。
?>
Copy after login

If you add a number after break, the execution process of the loop statement will change.

Example:

<?php
/**
* break 退出循环的例子
* edit by bbs.it-home.org
*/

while($i++<10){
echo '$i='.$i;
while($j++<5){
if($j==2){
break 2;//跳出两层循环代码的执行。
}
echo '  $j='.$j;
}
echo '‘;
}
}
?>
Copy after login

Code description: $i=1 $j=1 ends the loop. When performing the first loop, when $i=1, the first-level while code of the loop body starts to be executed. Following the output of $i=1, the second-level while loop is entered. After the if judgment, $j is not equal to 2. Do not execute the break 2; statement in the if body block, and then execute the output statement to output $j=1 and then perform the second judgment of the second-level loop. At this time, $j=2 executes break 2 in the if body;

Note: The break here is followed by the number 2, indicating that it is to exit the two-layer loop body code, that is, the first and second layer of loop bodies are jumped out of the judgment loop, which produces the above output result: $i= 1 $j=1.

2, continue (int); statement //The execution results of the continue statement and the break statement are similar. Continue skips several levels of loops, ignores the code segments in the skip loop body, and directly executes to the end of the loop body for the next loop. Like the break statement, it can also be used in in while, do...while, for, foreach, switch statements.

Example:

<?php
/**
* 跳过循环 continue
* edit by bbs.it-home.org
*/

while($i++<5){
if(++$j==2){
continue;
}
echo '$i='.$i.'‘;
}
?>
Copy after login

Code description: If the if judgment statement is not added to the while loop body, the output result is: $i=1 $i=2 $i=3 $i=4; but if the if judgment statement is added, the output result is: $i=1 $ i=3 $i=4, this is the impact of the continue statement , the continue statement is skipped when $j++ gets 2, that is, when $j=2, this loop will execute continue, skip the while loop body statement, and return directly to the end of the loop body (that is, jump to the loop body head) continue to the next A loop operation, when a number is added after the continue statement, the output result will change again.

Example:

<?php
while(++$a<3){
while(++$b<4){
if($b==2){
continue 2;
}
echo '  $b:'.$b;
}
$b=0;
echo '$a:'.$a;
echo '
‘;
}
Copy after login

Code description: Output result: $b=1 $b=3 $a=2 Execute the outer loop first, $a=1, the condition is "true", execute the second loop $b=1, the condition is also "true", after If judged, if it is "false", the continue statement will not be executed, and echo ' $b:'.$b; will be executed. This statement outputs $b:1; then the inner while loop, where $b is incremented, $b=2, the inner while condition continues to be "true", continues to execute the inner loop, and enters the if Judgment, the result if condition is judged to be "true" and executed The statement "continue 2;" directly skips the two layers of loop body code and comes to the end (that is, the head) of the parent loop. The loop body code in the parent and child layers will not be executed, and then continue to judge the parent layer loop. The condition $a=2 is less than 3 is true, Enter the inner loop again. At this time, the inner loop condition of $b=3 is established (true). If the if judgment condition is "false", "continue 2;" will not be executed, and the "echo ' $b:'.$b;" statement will be executed. , output $b=3, and then continue to judge the inner while loop Condition, here $b=4, the loop condition is not established, the inner while loop is ended, and “$b=0;echo’$a:’.$a;echo’ is executed. ‘;” These three statements then go to the outer while loop, and the condition is judged again. Here $a is already equal to 3, the condition is not established, and the outer loop ends.

Note: The difference between break and continue is that they can both add specific numbers at the end, but break exits the loop body determined by the number following it when the condition is established (if there is no number, it exits the current loop body) ,and continue is to jump out of several layers of loop bodies determined by the number following it when the condition is established (if there is no number, exit this loop body), the code segment in the loop body is not executed, and goes to the end of the loop body (i.e. head) Continue to loop condition judgment;

In short, break is to exit the loop body execution, while continue is to skip the execution of a certain level of loop body that meets the judgment condition statement, go to a certain level of loop, and continue to execute the loop body until the loop condition is not established to end the loop statement.

3, exit() and die() statements die is an alias for exit. They have the same function, which is to end the execution of the entire script. They are often used in situations such as entering data into the database and determining whether a certain file exists.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template