PHP3 Manual
PrevChapter 6. Language constructsNext

WHILE

WHILE loops are the simplest type of loop in PHP 3. They behave just like their C counterparts. The basic form of a WHILE statement is:

    WHILE(expr) statement

The meaning of a WHILE statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the WHILE expression evaluates to TRUE. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration (each time PHP runs the statements in the loop is one iteration). Sometimes, if the WHILE expression evaluates to FALSE from the very beginning, the nested statement(s) won't even be run once.

Like with the IF statement, you can group multiple statements within the same WHILE loop by surrounding a group of statements with curly braces, OR by using the alternate syntax:

    WHILE(expr): statement ... ENDWHILE;

The following examples are identical, and both print numbers from 1 to 10:

/* example 1 */
$i=1;
while ($i<=10) {
    print $i++;  /* the printed value would be $i before the increment (post-increment) */
}

/* example 2 */
$i=1;
while ($i<=10):
    print $i;
    $i++;
endwhile;


PrevHomeNext
ELSEIFUpDO..WHILE