$i=5;
do{
echo $i."<BR>";
$i=$i+1;
} while ($i < 15);
Output is from 5 to 14 numbers.
$i=1;
do{
echo $i;
echo "<br>";
} while ($i > 1);
We can see in the above code that even though the $i value is 1 which is not true as per the condition set by the loop still the script will print value of $i once as the condition is checked at the end of the loop. To compare between these two types of loops check this code.
<?Php
$i=1;
do{
echo $i; // Out put will be there
echo "<br>";
} while ($i > 1);
echo "<hr>";
$i=1; // we assigned a value to the variable again here
while($i > 1){
echo $i; // No output as loop will not be executed
echo "<br>";
}
?>$i=5;
do{
echo $i;
echo "<br>";
$i=$i+1;
if($i>10){break;}
} while ($i < 15);
$i=5;
do{
$i=$i+1;
if($i==10){continue;} // Value 10 is not printed.
echo $i."<BR>";
} while ($i < 15);
Here output will be from 6 to 15 without printing the number 10. $i=1;
$j=1;
do {
echo $i*$j ."|";
$j=$j+1;
} while($j<=10);
Output
1|2|3|4|5|6|7|8|9|10|
$i=1;
$j=1;
do { // Outer loop
do { // Inner loop
echo $i*$j ."|";
$j=$j+1;
} while($j<=10); // end of inner loop
$j=1; // reset value to 1
$i=$i+1; // increment
echo "<br>"; // Line break
} while($i<=10) // end of outer loop
Output is here
1|2|3|4|5|6|7|8|9|10|
2|4|6|8|10|12|14|16|18|20|
3|6|9|12|15|18|21|24|27|30|
4|8|12|16|20|24|28|32|36|40|
5|10|15|20|25|30|35|40|45|50|
6|12|18|24|30|36|42|48|54|60|
7|14|21|28|35|42|49|56|63|70|
8|16|24|32|40|48|56|64|72|80|
9|18|27|36|45|54|63|72|81|90|
10|20|30|40|50|60|70|80|90|100|
PHP Loops
PHP While loop
For loop
Switch for matching code block in PHP
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| momas | 18-12-2014 |
| good | |