The Do While Loop
The PHP While loop is used to execute a block of code until given condition is true, i.e. loop till certain criteria is keep on meeting. Whereas The do…while loop type statement will execute a block one time at least- it then will go through execute code and loop till specified condition is true. This is the only difference between While loop and do…While loop
Structure of Do…While in PHP
do
{
//block of code here;
Increment;
}while (condition);
Example of Do While
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?php
$i = 0;
Do
{
print $i.“<br>”;
$i++;
}
while( $i < 5)
?>
|
More real example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?php
$price = 5;
print “<table border=”1″ align=”center”>”;
print “<tr><th>Quantity</th>”;
print “<th>Price</th></tr>”;
$i=10;
Do{
print “<tr><td>”;
print $i;
print “</td><td>”;
print $price * $i;
print “</td></tr>”;
$i = $i + 10;
}
While ($i <= 100)
?>
|
Other Loops: For Loop While Loop | Foreach Loop
Leave A Comment?