|
While loops are very similar to if statements except the process code will run again and again (loop) while the condition evaluates to true. In addition, while loops are normally used when you do not know the number of iterations you must loop.
Here's some code:
$dollars = 15;
while ($dollars > 10)
{
echo "Hey, doll, I'm loaded with cash!";
echo "I have ".$dollars." dollars.<br>";
$dollars=$dollars-1;
}
The above code, like an if statement, evaluates the condition. If the condition evaluates as true, the process code will run. Unlike an if statement, the process code will keep looping until the condition evaluates as false.
Also notice that the condition is tested first and then the process code is run (if true). This is differerent than the Do While Loop.
Here's another example:whileloops.php
|