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.
Syntax:
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).
<?php
    $fruits = array("Apple", "Banana", "Orange", "Mango");
    foreach ($fruits as $key=>$value) {
      echo "$key : $value <br>";
    }
?>

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

Comments

Leave a Reply

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

59709