Home > Backend Development > PHP Tutorial > php--while/do-while

php--while/do-while

伊谢尔伦
Release: 2016-11-24 09:25:54
Original
1418 people have browsed it

while

(PHP 4, PHP 5)

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

while (expr)
    statement
Copy after login

The meaning of the while statement is very simple. It tells PHP to repeatedly execute the nested loop statement as long as the value of the while expression is TRUE. The value of the expression is checked each time the loop starts, so even if the value changes during the loop statement, execution of the statement will not stop until the end of the loop. Sometimes if the value of the while expression is FALSE at the beginning, the loop statement will not be executed even once.

Like if statements, you can use curly braces to enclose a group of statements in a while loop, or use alternative syntax:

while (expr):
    statement
    ...
endwhile;
Copy after login

The following two examples are exactly the same, both display the numbers 1 to 10:

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

do -while

(PHP 4, PHP 5)

do-while loop is very similar to while loop, except that the value of the expression is checked at the end of each loop instead of at the beginning. The main difference from a normal while loop is that the do-while loop statement is guaranteed to be executed once (the truth value of the expression is checked after each loop), but in a normal while loop this is not necessarily the case (the truth value of the expression Checked at the beginning of the loop, if it is FALSE at the beginning, the entire loop terminates immediately).

do-while loops have only one syntax:

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

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

Experienced C language users may be familiar with a different do-while loop usage, which is to put the statement in do-while(0) and use the break statement inside the loop to end the execution loop. The following code snippet demonstrates this method:

<?php
do {
    if ($i < 5) {
        echo "i is not big enough";
        break;
    }
    $i *= $factor;
    if ($i < $minimum_limit) {
        break;
    }
    echo "i is ok";

    /* process i */

} while(0);
?>
Copy after login

Don’t worry if you don’t understand it right away. You can still write powerful code without using this "feature". Since PHP 5.3.0, you can also use goto to break out of loops.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template