For just getting started PHP children's shoes, in the loop body in the role of continue and break is always unclear what is the meaning, how to use, what is the difference between the two?
Let's say a few examples, but they are very well differentiated.
1<?PHP2 3 $tmpVar= [];4 for($a= 1;$a++;$a<10){5 if($a= = 2){6 Continue;7 } 8 Array_push($tmpVar,$a);9 }Ten Print_r($tmpVar); One A //The results are as follows - Array(1, 3,4,5,6,7,8,9);//here the continue jumps out of a $ = = 2 O'Clock Loop
To summarize: The Continue command terminates a single loop of the loop body, and the code continues to execute down.
Continue and continue1 are the same, continue2 refers to jumping out of two cycles, and the code goes down.
For example:
1<?PHP2 3 $tmpVar= [];4 for($a= 1;$a++;$a<10){5 if($a= = 2){6 Continue;7 } 8 Array_push($tmpVar,$a);9 }Ten Print_r($tmpVar); One A //The results are as follows - Array(1, 3,4,5,6,7,8,9);//here the continue jumps out of a $ = = 2 O'Clock Loop
Now let's talk about the use of break
1<?PHP2 3 $tmpVar= [];4 for($a= 1;$a++;$a<10){5 if($a= = 2){6 Break;7 } 8 Array_push($tmpVar,$a);9 }Ten Print_r($tmpVar); One A //The results are as follows - Array(1);//This break terminates the loop body.
Summary: Break Terminates the traversal of the loop body, and the code then runs;
The difference between PHP continue and break