PHP While Loops
PHP while loops allow programs to execute the same lines of code repeatedly, this is important for many things in programs. In PHP you often use them to layout tables in HTML and other similar functions.
Syntax
<?php
while (condition){
statement
}
?>
Example
<?php
#example
$i = 1;
while ($i <= 10) {
echo $i++;
}
#example 2
$i = 1;
while ($i <= 10):
echo $i;
$i++;
endwhile;
?>
In This example that prints the numbers from 1 to 10. $i starts out as 1. When the while loop is encountered, the expression $i <= 10. If it is true, then it executes what is in the curly braces. The echo statement will print 1, and then add one to $i. The program will then go back to the top of the loop and check the expression again. Since it is true again, it will then return 1 and add one to $c. It will keep doing this until $i is equal to 10, where the statement $i<=10 is false. After that, it finishes.
do while loop
The do while loop is similar in syntax and purpose to the while loop. The do/while loop construct moves the test that continues the loop to the end of the code block. The code is executed at least once, and then the condition is tested. For example:
Example
<?php
$i = 10;
do {
echo 'i am do while loop.';
} while ($i < 5);
?>
Even though $i is greater than 10, the script will echo "i am do while loop." to the page one time.
<?php
$number = 5;
$factorial = 1;
do {
$factorial *= $number;
$number = $number - 1;
} while ($number > 0);
echo $factorial;
#Output: 120
?>


