PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

"while" Statements

This section describes how 'while' statements works in PHP. One or more statements are repeatedly executed as long as the specified condition is true.

The "while" statement is the most commonly used loop statement in PHP. There are 3 forms of "while" statements supported in PHP:

1. Single-statement "while":

   while (condition) statement;

2. Multi-statement "while":

   while (condition) {
      statement;
      statement;
      ...
   }

3. Multi-statement "while ... endwhile":

   while (condition): 
      statement;
      statement;
      ...
   endwhile;

All forms of "while" statements are executed like this:

Step 1: Evaluate "condition" as a Boolean value.

Step 2: If "condition" returns TRUE, continue with Step 4.

Step 3: If "condition" returns "FALSE", terminate the loop.

Step 4: Execute the statement or statements enclosed in the loop.

Step 5: Continue with Step 1.

Of course, you can use the "break" statement inside the loop to terminate the loop immediately.

Last update: 2005.

Sections in This Chapter

"while" Statements

"while" Statement Examples

"for" Statements

"for" Statement Examples

"do ... while" Statements

"break" and "continue" Statements

Dr. Herong Yang, updated in 2009
"while" Statements