A FOREACH loop is phrased like this: FOREACH (array as value) { what to do; }
Here is an example of a FOREACH loop:
<?php
$a = array(1, 2, 3, 4, 5);
foreach ($a as $b)
{
print $b . " ";
}
?>
Once you understand that concept you can then use the FOREACH loop to do more practical things. Let's say an array contains the ages of 5 family members. Then we will make a FOREACH loop that will determine how much it costs for each of them to eat on a buffet that has varied prices based on age. We will use the following pricing system: Under 5 is free, 5-12 years costs $4 and over 12 years is $6.
<?php
$t = 0;
$age = array(33, 35, 13, 8, 4);
foreach ($age as $a)
{
if ($a < 5)
{$p = 0;}
else
{
if ($a <12)
{$p = 4;}
else
{$p = 6;}
}
$t = $t + $p;
print "$" . $p . "<br>";
}
print "The total is: $" . $t;
?>

