PHP for loop is used when the specified condition is predefined.
Syntax:
1 2 3 4 5 |
for (initialization; condition; increment/decrement ) { code to be executed; } |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php echo " Print counting using for loop"."<br>"; for($i=1;$i<=5;$i++){ echo $i."<br>"; } ?> </body> </html> |
Output
Print counting using for loop
1
2
3
4
5
PHP Nested For loop
PHP Nested for loop is used inside the loop that is called nested for loop.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php echo " Print prymid using Nested for loop"."<br>"; for($i=1;$i<=5;$i++){ for($j=1;$j<=$i;$j++){ echo "*"; } echo "<br>"; } ?> </body> </html> |
Output
Print pyramid using Nested for loop
*
**
***
****
*****