Php -- break/continue

Source: Internet
Author: User
Break ends the execution of the current for, foreach, while, do-while or switch structure. The break can accept an optional numeric parameter to determine the number of loops to jump out.

Break

(PHP 4, PHP 5)

Break ends the execution of the current for, foreach, while, do-while or switch structure.

The break can accept an optional numeric parameter to determine the number of loops to jump out.

 \ N ";}/* use the optional parameter */$ I = 0; while (++ $ I) {switch ($ I) {case 5: echo" At 5
\ N "; break 1;/* only exits switch. */case 10: echo" At 10; quitting
\ N "; break 2;/* exit switch and while loop */default: break;}?>

Continue

(PHP 4, PHP 5)

Continue is used in the loop structure to skip the remaining code in this loop and start to execute the next loop when the condition value is true.

Note: In PHP, the switch statement is considered to be a loop structure that can use continue.

Continue accepts an optional numeric parameter to decide to skip multiple cycles to the end of the loop. The default value is 1, that is, jump to the end of the current loop.

 \n";        while (1) {            echo "Middle
\n"; while (1) { echo "Inner
\n"; continue 3; } echo "This never gets output.
\n"; } echo "Neither does this.
\n"; }?>

Omitting the semicolon after the continue will lead to confusion. The following example shows that you should not do this.

 

The expected result is:

0134

The actual output is:

2

Because the entire continue print "$ I \ n"; is evaluated as a single expression, therefore, the print function is called only when $ I = 2 is true (the print value is passed to continue as an optional numeric parameter ).

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.