|
| HOW TO USE while LOOPS IN PHP
A while loop repeats a certain block of code as long as a certain condition is true. As soon as it is not, it stops repeating that block of code.
You simply set a certain condition and this is tested at the top of the loop. The general format is:
while(certain condition)
{
execute this code;
}
Example:
$price = 5;
while ($price <= 10 )
{
echo "Still cheap";
$price++;
}
We set our variable $price with a start value of 5. We test to see if $price is less than or equal to 10. If it is, we echo Still cheap and the $price++ increments the $price by 1 (ie. $price + 1 making $price now 6). The loop then checks if $price is less than or equal to 10. If it is (it is in this case as $price is now 6) we echo Still cheap and the $price++ increments the $price by 1 (ie. $price + 1 making $price now 7).
This loop continues until $price is less than or equal to 10. Meaning, if it is 11, then the loop stops executing the code {between braces}.
|
|