Php--while/do-while

Source: Internet
Author: User

While

(PHP 4, PHP 5)

The while loop is the simplest type of loop in PHP. It behaves the same as while in the C language. The basic format of the while statement is:

while (expr)    statement

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.

As with the IF statement, you can enclose a group of statements in curly braces in a while loop, or substitute syntax:

while (expr):    statement    ... endwhile;

The following two examples are exactly the same, showing the numbers 1 to ten:

<?php/* Example 1 * * $i = 1;while ($i <=) {    echo $i + +;  /* The printed value would be                    $i before the increment                    (post-increment) */}/* Example 2 */$i = 1;while ($i <= 10 ):    print $i;    $i ++;endwhile;? >

Do-while

(PHP 4, PHP 5)

The Do-while loop is very similar to the while loop, except that the value of the expression is checked at the end of each loop rather than at the beginning. The main difference from the normal while loop is that the Do-while Loop statement is guaranteed to execute once (the true value of the expression is checked at the end of each loop), but not necessarily in the normal while loop (the expression truth is checked at the beginning of the loop, if it starts with FALSE The entire loop is terminated immediately).

The Do-while loop has only one syntax:

<?php$i = 0;do {   echo $i;} while ($i > 0);? >

The above loop will run exactly once, because after the first loop, when the truth of the expression is checked, the value is FALSE ($i not greater than 0) and causes the loop to terminate.

Experienced C users may be familiar with another different do-while loop usage, placing the statement in Do-while (0) and ending the execution loop with a break statement inside the loop. The following code snippet demonstrates this approach:

<?phpdo {    if ($i < 5) {        echo "I am not big enough";        break;    }    $i *= $factor;    if ($i < $minimum _limit) {break        ;    }    echo "I is OK";    /* Process I */} while (0);? >

Don't worry if you don't understand it right away. Even without this "feature" you can write powerful code. From PHP 5.3.0, you can also use Goto to jump out of the loop.

  • Related Article

    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.