PHP loop statements

Source: Internet
Author: User

1. Break: indicates the process of stopping the current for, while, do... while, and switch. A number can be provided later to indicate the layer to which the switch flow is redirected.

For example:

<? PHP

$ I = 0;

While (++ $ I ){

Switch ($ I ){

Case 4:
Echo "quit to 4. <br> ";
Break; // jump out of the switch statement
Case 9:
Echo "quto to 9. <br/> ";
Break 2; // There is a 2-layer hop. The number in the while loop cannot exceed the actual number of layers. If it is written as break 3, the system reports an error.
Default:
Break;
}
}
Echo 'over! & Nbsp; $ I = '. $ I;
?>

Result:

Quit to 4.
Quto to 9.
Over! $ I = 9

 

2. Continue statement:Jump out of the remainder of this loopCodeAnd determine the next cycle when the condition is true

<? PHP

For ($ I = 0; $ I <13; $ I ++ ){
If ($ I = 5 ){
Continue; // jump out of the remaining code in this loop and judge whether this condition is true for the next loop
}
Echo '$ I ='. $ I. "<br/> ";
}

Echo "over ";

?>

Result:

$ I = 0
$ I = 1
$ I = 2
$ I = 3
$ I = 4
$ I = 6
$ I = 7
$ I = 8
$ I = 9
$ I = 10
$ I = 11
$ I = 12
Over

You can also add numbers after continue:

<? PHP

For ($ I = 0; $ I <2;$ I ++ ){//@

For ($ j = 1; $ j <4; $ J ++ ){

If ($ J = 2 ){

Continue 2; // After jumping out of the Layer 2 loop, execute $ I ++ //@
}

Echo '$ I ='. $ I. '$ J ='. $ J. '<br/> ';
}
}
Echo "over ";
?>

Result:

$ I = 0 $ j = 1
$ I = 1 $ j = 1
Over

3. GOTO statement: Jump can only be performed in the same file or scope

Syntax: goto tag;

Tags:

//... Statement

<? PHP

Goto;
Echo "AA ";
A:
Echo "BB ";

?>

Output: bb

 

 

 

 

 

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.