PHP » Control Structures » while

Syntax:
while (condition) {
   // block to repeat
}

Repeats a block code as long as a condition is met.

A while loop is often useful when a block of code needs to be repeated a number of times that is not known in advance.

Examples

Code:
<?php

$fruits = array("apple", "banana", "orange");

while ($fruit = current($fruits)) {
   print "$fruit ";
   next($fruits);
}

?>
Output:
apple banana orange
Explanation:

The code shows how a while loop can be used to go through all elements in an array.

See Also: