PHP Foreach Statement

PHP Foreach used to traverse to each array element. In the foreach loop will process each item in the array using the same operations until it has cycled through each item in the array. When foreach first starts executing, Automatically the internal array pointer reset to the first element of the array.

Foreach Syntax :

<?php
foreach (array_name as temporary_variable)
{
do something with temporary_variable
}
?>

array_name this

parameters used to store array name.

temporary_variable

this parameters used to store array temporary.

Foreach Example :


<?php
$shoppingList = array('wine', 'fish', 'bread', 'grapes', 'cheese');
foreach ($shoppingList as $item)
{
echo $item;
}
?>

The above example loops through the $shoppingList array and displays the name of each item.





Content