How do I use the break keyword in PHP?

Source: Internet
Author: User
I know that break in PHP jumps out of the loop, so what does it mean to add 2 or 3 after break? For example: Break 2; Excuse me, what do you mean by this? and break; What's the difference???? Can you give me an example? Thank you thank thank you thank you thank you!!! ...

In nested loops or multiple switch structures can jump out of a few layers of meaning, by default, break can not write.
For example:

foreach ($arr as $v) {    foreach ($v as $_v) {        if ($_v== ' a ') break 2;}    }

This is a two-dimensional foreach traversal, and when the element value is a, jump out of the 2-layer traversal, which means that the foreach traversal is no longer available even if the data is not traversed.

Note that if is not counted in the hierarchy, only for/foreach/while/do-while/switch inside with break can jump out.

$a =10;while ($a) {    switch ($a) {case        7:echo ' haha ';        Case 6:echo ' yoyo ';        Default:            if ($a!=3) {                  echo ' Nini ';            } else{                 break2;            }            break;    }    $a--;}

The statement I explained, first defines a variable $ A, which is assigned a value of 10. A while loop is then performed, and the condition of the loop is that when $ A is a valid value, the valid value here means that a $ A value is not false (that is, $ A cannot be 0) because there is a $ A at the end of the while--, the value of $ A is decremented, and there is always a decrease in the loop to 0.
There is a switch statement in the while loop, the value of the current $ A to judge, if the 7 when printing haha, 6 when the print Yoyo, after the end of the print switch, the default is determined if $ A value is not 3, so long print a Nini, Then end the switch, $a the next loop after minus 1, and if $ A equals 3, then break 2 jumps out of the double loop, and then the while loop is ended, no matter how much a $ A has not been reduced to 0.

Continue this jump out of the Loop keyword can be added to the number of? such as: Continue 2; , skipping two loops. I tried it and I found out that it was an error.
Thank you thank you thank you thank you!!!!!! Thank you thank you thank you!!!!!!! Thanks thanks!!!!!

It is possible to add, continue also to be used with the IF condition statement, and break will terminate the entire loop when the break is in a different place, while the continue is the next loop to end the loop.

Break Jump out of the current loop or switchbreak N; Jumps out of the N-layer loop. Switch $case {case "1": Breack;case "2": Bereak 2;}

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.