Forum Moderators: coopster

Message Too Old, No Replies

Learning Php and need help

         

ssqp

4:35 am on Jul 13, 2009 (gmt 0)

10+ Year Member



I'm learning PHP as a beginner. Recently I'm studying arrays.

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.

idfer

5:08 am on Jul 13, 2009 (gmt 0)

10+ Year Member



You have a semi-colon at the end of your foreach statement:

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.