Libin Abraham
Libin Abraham

Reputation: 100

Laravel 5.2 proper foreach syntax

what is the difference between the following syntax ?

foreach($products as $product) {
// loop body
}

and

foreach($product as $product) {
 //loop body
}

got error some time when second one is used. Thanks

Upvotes: 1

Views: 34

Answers (1)

Nour
Nour

Reputation: 1497

This syntax is wrong

 foreach($product as $product) {
 //loop body
 }

As we loop through the $products and get out every single $product of it so we pass the array first then we follow it by the name of the single variable we want to use later your foreach loop should be like this:

foreach($products as $product){
 //loop body 
}

Upvotes: 1

Related Questions