PHP foreach Loop

The foreach loop – Loops through a block of code for each element in an array or each property in an object.

The foreach Loop on Arrays

The most common use of the foreach loop, is to loop through the items of an array.

Example

Loop through the items of an indexed array:

$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $x) {
  echo "$x <br>";
}

For every loop iteration, the value of the current array element is assigned to the variable $x. The iteration continues until it reaches the last array element. Continue reading PHP foreach Loop