Tags
PHP
Asked 7 years ago
28 Sep 2016
Views 819
Phpworker

Phpworker posted

foreach issue


$product_list=array("Apple mac","Dell Laptop","Hp Laptop");//putting some dummy productname instead of real one

foreach($product_list as $value);
{
     echo $value."<br/>";
}


it should be give result

Apple mac
Dell Laptop
Hp Laptop


but result i get is

Hp Laptop

so whats wrong ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

Wrong is ";" put after foreach($product_list as $value)

so foreach($product_list as $value); means it iterate loop and end the stantment because of ";" but dont process next '{ echo $value."<br/>"; }' code .

so you need like

foreach($product_list as $value){ 
    echo $value."<br/>";
}



for the best understanding
foreach($product_list as $value);
means
foreach($product_list as $value){}


so it jst output you array ' s last element value
yes it work for me - Phpworker  
Sep 28 '16 17:53
jagdish

jagdish
answered Nov 30 '-1 00:00

its simple , you accidentally put a semicolon after the foreach statement , pls remove it . and it solve
Post Answer