|
PHP supports loops which are similar to C, while are while(){},
do{} while(), and for().The while() loop will loop until the condition
is TRUE. For example:
While () loop
in PHP |
| <?php
$value=0; //our variable
while($value<=10){
print(" $value = ".($value*$value));
print("<br />\n");
$value=$value+1;
}
?>
|
Executed code |
|
0 = 0
1 = 1
2 = 4
3 = 9
4 = 16
5 = 25
6 = 36
7 = 49
8 = 64
9 = 81
10 = 100
|
The following shows an example of a for(), do {} while (), and
while () loops:
Loops in PHP |
| <?php
for ($i=0;$i<11;$i++)
{
$sqr_value=$i*$i;
print "<BR>$i $sqr_value";
}
$i=0;
do
{
$sqr_value=$i*$i;
print "<BR>$i $sqr_value";
$i++;
} while ($i<11);
$i=0;
while ($i<11)
{
$sqr_value=$i*$i;
print "<BR>$i $sqr_value";
$i++;
}
?>
|
Executed code |
|
0 0 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100 0 0 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100 0 0 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100 |
|