The foreach loop is used to traverse the array elements. The foreach loop works only on arrays and is used to loop through each key/value pair in an array.

 

Syntax:

foreach ($array as $value) {
  	code to be executed;
}

 

Example:

$colors = array("red", "green", "blue", "yellow", “orange”);
foreach ($colors as $value) {
  echo "$value <br>";
}