PHP loop structure How to use the example of the detailed

Source: Internet
Author: User
The meaning of the while statement is simple, and it tells PHP to repeatedly execute the loop statements in the nested if the value of the while expression is TRUE. The value of the expression is checked every time the loop is started, so even if the value changes in the Loop statement, the statement does not stop executing until the end of the loop. Sometimes a loop statement does not execute once if the value of the while expression starts with FALSE.

Loop structure: Repeat the operation according to the stipulated conditions, pay attention to the stopping condition, otherwise it is prone to die cycle.

1.while loop, satisfies the condition, then executes the loop body repeatedly

While expression {

Loop body

}

<?php$i=0;while ($i <7) {    $i + +;    echo $i, ' <br> ';}

Output: 1 2 3 4 5 6 7

2.do...while cycle, first execute once again to make judgment

do{

EXECUTE statement

}while-expression

<span style= "FONT-SIZE:18PX;" ><?php$i=7;do{    $i + +;    echo $i;} while ($i <7);</span>

Output: 8

3.for Cycle

for (initial value; conditional expression; increment) {

Loop body

}

<?phpfor ($i =0; $i <10; $i + +) {    echo ' Hello World ', ' <br> ';}

4. Several loops-related statements

Break stop and exit, BREAK2, BREAK3 respectively exit 2 layer loop, exit 3 layer Loop

<?php$i=0;do{    $i + +;    echo $i;    if ($i ==4) {break        ;    }} while ($i <7);

Output: 1 2 3 4

Continue skip this cycle

<?php$i=0;do{    $i + +;    if ($i%2==0) {        continue;    }    echo $i;} while ($i <10);

Output 1 3 5 7 9

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.