The difference between PHP continue and break

Source: Internet
Author: User
Tags terminates

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

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.