Web Info and IT Lessons Blog...

Thursday, 20 November 2014

Nested Loops in PHP

The previous lesson of PHP Lessons Series was about Multidimensional Arrays in PHP and in this lesson we will learn about Nested Loops in PHP.

Nested Loops in PHP

A nested loop in PHP is the one which is contained inside another loop. The basic structure of a nested loop is given below:


Loop{  // Loop Starts

 Loop{  // Loop Starts
  
 }  // Loop Ends
 
}  // Loop Ends

The above example shows a loop nested inside another loop. We can have various combinations of nested loops and a few of those are discussed below:

Nested for loop in PHP

The basic structure of a nested for loop is given below:


for(Loop Conditions){  // For Loop Starts

 for(Loop Conditions){  // For Loop Starts
  
 }  // For Loop Ends
 
}  // For Loop Ends

The above example code shows a for loop nested inside another for loop. The basic flow of the above for loop is obvious from the above structure and our previous knowledge of a for loop. First of all the condition of the outer for loop will be checked and if the condition returns true, the code inside the outer for loop which in this case is a nested for loop will be executed. Now the condition of nested for loop will be checked and if true, the code inside the nested for loop will be executed. After the execution of code inside the nested for loop, the condition of the nested for loop will be checked again for the second iteration and if true the code will be executed once again. This process will continue until the condition of the nested for loop remains true. When the condition becomes false, the flow will shift back to the outer for loop and the condition of the outer loop will be checked. If the condition of the outer for loop returns true again, the complete process of the nested for loop will repeat again for the second iteration of the outer for loop. This process will repeat itself again and again until the condition of the outer for loop becomes false.

Use of a for loop to echo star pattern in PHP


for($i=0;$i<10;$i++){
 for($j=0;$j<10;$j++){
  echo '* ';
 }
 echo '<br />';
}

In the above code, for the first iteration of the outer for loop the inner loop will run 10 times and will print a total of 10 stars. As there will be a total of 10 iterations of the outer for loop so, the total number of stars printed will be 100.

Use of a for loop to echo values of a multidimensional array


$multidimensional_array=array(array('John','England',
                                    'London','Laptop'),
         array('Malcom','United States',
                                    'New York','LCD'));         
for($i=0;$i<2;$i++){
 for($j=0;$j<4;$j++){
  echo $multidimensional_array[$i][$j].' ';
 }
}

In the above mentioned code, the outer for loop will have 2 iterations as there are 2 arrays in the main multidimensional array and the inner for loop will have 4 iterations as there are 4 indexes of each array inside the main multidimensional array.

Nested while loop in PHP

The basic structure of a nested while loop is given below:


while(Loop Conditions){  // While Starts

 while(Loop Conditions){  // While Starts
  
 }  // While Ends
 
}  // While Ends

Just like a nested for loop, a nested while loop has a while loop nested inside another while loop but that's not the only combination we can have for a nested while loop. We can also have a while loop nested inside a for loop and vice versa. Similartly we can nest a do while loop and a foreach loop in PHP.

Nested do while loop in PHP

The basic structure of a nested do while loop is given below:


do{  // Do While Starts

 do{  // Do While Starts
  
 }while(Loop Conditions)  // Do While Ends
 
} while(Loop Conditions) // Do While Ends

Nested foreach loop in PHP

The basic structure of a nested foreach loop is given below:


foreach(Loop Conditions){  // Foreach Starts

 foreach(Loop Conditions){  // Foreach Starts
  
 }  // Foreach Ends
 
} // Foreach Ends

No comments:

Post a Comment