★ wanayoo — archive 1999 http://www.php.net/manual/control-structures.while.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Control Structures
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Control Structures
* if
* else
* elseif
* Alternative syntax for control structures
* while
* do..while
* for
* foreach
* break
* continue
* switch
* require
* include
* require_once
* include_once
Manual: while
View the source code for this pageSearch the site



Previous page
 Alternative syntax for control structures
 Updated
Mon, 07 Aug 2000
do..while 
Next page


while

while loops are the simplest type of loop in PHP. 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;
     


User Contributed Notes: while



17-May-2000 10:19
non-stop loop is a common mistake for many programmers, but how does php handle it? does it stop after a period of time or the user has to find a way to do that?


bkosse@iname.com
28-Jun-2000 03:58
There's an option in php.ini to limit both the memory consumption and runtime of a PHP script (see max_execution_time).


lgd@mailroom.com
29-Jul-2000 02:30
How do I use multiple conditions on a WHILE statement? I use this: <pre>while($myrow = mysql_fetch_array($query) && ($contar < 5)</pre> and it doesn't work.


jednet@inept.net
10-Aug-2000 07:44
You can use multiple conditions, you just need to remember the order of opperations, and put things in parenthasis.

while(($row = mysql_fetch_row($results)) && ($count++<10));



 About Notes


Previous page
 Alternative syntax for control structures
 Updated
Mon, 07 Aug 2000
do..while 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.