PHP foreach loop

The foreach loop is used to get each element value in an array. foreach loop is used to loop through each key/value pair in an array.
foreach ($array as $key=>$value) {
  code to be executed;

Example :
PHP program code to print array elements using foreach loop.The below example will give output the values of the given array ($fruits).
    $fruits = array("Apple", "Banana", "Orange", "Mango");
    foreach ($fruits as $key=>$value) {
      echo "$key : $value <br>";

0 : Apple
1 : Banana
2 : Orange
3 : Mango


Leave a Reply

Your email address will not be published. Required fields are marked *