4
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by vivosmith
0

@vivosmith

I was working on some code, when I ran into this: foreach ($products as $product). One problem, it was not defined. How could this be?

The link you provided didn't work

so for your example: foreach ($products as $product)

You can used this:

<?php
$products = array(1, 2, 3, 4);
foreach ($products as &$product) {
    $product = $product * 2;
}
unset($product); 
?>

If you really want to learn and know how that example works then read this:

http://php.net/manual/en/control-structures.foreach.php

This will give you an idea on how that foreach() function works.

0

Basically you're saying to loop through the $products array, and for each element in the array, temporarily make it accessible via the $product variable. Do something with this $product variable, and then loop. This way you have access to a single variable name that you can keep reusing for each element in the array. Kinda sorta. I'm bad at explaining.

0

I figured it out. I believe the foreach statement is creating an array from the previous variable. Am I correcting in saying it goes from variable to array, or is it the opposites?

0

I see. The catergories variable is a query, and the array variable category stores the results. Thanks

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.