PHP For Each Loop with Example

PHP Foreach Loop Tutorial


The ForEach Loop is mainly used for Looping through each key/value pair in an Array..You can define two parameter inside foreach separated through "as" keyword. First parameter must be existing array name which elements or key you want to display.At the Position of 2nd parameter, could define two variable: One for key(index) and another for value.

Syntax of ForEach loop:

   foreach ($array as $value)
     {
   code to be executed;
 
   }
 You can use ForEach Loops in this way as well.
       foreach($array_name as $key=>$value)
              {
             code be executed;
               }
 
Example of ForEach Loop:
 

  Output: