In this post, we will learn about nested for loop in PHP and different ways to use them in a program.
What is for loop
The for
keyword indicates a loop in PHP. The for
loop executes a block of statements repeatedly until the specified condition returns false.
1 2 3 4 5 6 |
for (initializer; condition; iterator) { //code block } |
What is Nested for loop
A nested loop is a loop within a loop, an inner loop within the body of an outer one. How this works is that the first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes. Of course, a break within either the inner or outer loop would interrupt this process.
Nested for loop Examples
Example 1: Write a PHP script using nested for loop that creates a chess board
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Code4Example</title> <style> table{ border:10px double red; padding:10px; border-spacing:1px; } table td{ width:50px; height:50px; border:1px solid red; } td.black{ background:#000; } td.white{ background:#eee; } </style> </head> <body> <?php echo "<table>"; for($row=1;$row<=8;$row++) { echo "<tr>"; for($column=1;$column<=8;$column++) { $total=$row+$column; if($total%2==0) { echo "<td class='black'></td>"; } else { echo "<td class='white'></td>"; } } echo "</tr>"; } echo "</table>"; ?> </body> </html> |
Output:
Example 2: Create a script to construct the following pattern, using nested for loop
1 2 3 4 5 6 7 |
* * * * * * * * * * * * * * * |
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 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Code4Example</title> </head> <body> <?php for($x=1;$x<=5;$x++) { for ($y=1;$y<=$x;$y++) { echo "*"; if($y< $x) { echo " "; } } echo "<br>"; } ?> </body> </html> |
Output:
Example 3: Write a program to create given pattern with * using for loop.
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 32 33 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Code4Example</title> </head> <body> <?php $n=5; for($i=1; $i<=$n; $i++) { for($j=1; $j<=$i; $j++) { echo ' * '; } echo '<br>'; } for($i=$n; $i>=1; $i--) { for($j=1; $j<=$i; $j++) { echo ' * '; } echo '<br>'; } ?> </body> </html> |
Output:
Example 4: write a program to create given pattern with * using for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Code4Example</title> </head> <body> <?php $n=5; for($i=1; $i<=$n; $i++) { for($j=1; $j<=$n; $j++) { echo ' * '; } echo '<br>'; } ?> </body> </html> |
Output:
Example 5: Create A Script To Construct The Following Pattern, Using A Nested For Loop. ** * * * ** * * * * * * * * ***
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Code4Example</title> </head> <body> <?php function print_pattern1($num) { // Outer loop handles number of rows for ($i = 0; $i < $num; $i++) { // inner loop handles number of columns for($j = 0; $j <= $i; $j++ ) { // Print stars echo "* "; } // go to new line after each row pattern is printed echo "<br>"; } } function print_pattern2($num) { // Outer loop handles number of rows for ($i = $num-1; $i >= 0; $i--) { //whitespace for($k = $num; $k > $i+1; $k-- ) { // Print stars echo " "; } // inner loop handles number of columns for($j = 0; $j <= $i; $j++ ) { // Print stars echo "* "; } // go to new line after each row pattern is printed echo "<br>"; } } print_pattern1(5); print_pattern2(5); ?> </body> </html> |
Output:
[…] You may also like: PHP Nested for loop Examples […]