Imran Omar Bukhsh Imran Omar Bukhsh - 3 months ago 18
PHP Question

Array Foreach Loop Prints Last Item Only

Why does the following only print one fruit?

$fruits = array('banana','apple','orange');
foreach($fruits as $fruit);
{
echo $fruit."<br>";
}


Output:

orange

Answer

Because you have an extra semicolon at the end of the foreach line. Remove it, and all should be well.

Comments