Forum Moderators: coopster
I tried to use the "foreach" function to list the flowers and prices in a table. Here's my code:
$array3 = array(
"Rose" => "5.00",
"Orchid" => "6.00",
"Daisy" => "7.00",
);echo "<br>";
echo "<table border='1' cellpadding='7'>";
echo"<tr><th>Flower</th><th>Price</th></tr>";
foreach($array3 as $flower => $price);
{
echo "<tr><td>$flower </td><td>$price </td></tr>";
}
echo "</table>";
But the table doesn't list all three flowers. It only lists "Daisy" and its price. Can anyone tell me what's wrong here?
Thanks.
foreach($array3 as $flower => $price); <-- common mistake
Remove that and you're good.
The gory details: The compiler interprets that semi-colon as an empty statement that is the body of your foreach statement. So PHP goes through the array executing nothing at each iteration. After the loop is finished $flower is Daisy, $price is 7.00, and the next block is executed once: echo "<tr><td>...".
Hope this helps.