Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Simple PHP Application Working With Functions Creating the Shirt Display Function

Johnatan Guzman
PLUS
Johnatan Guzman
Courses Plus Student 2,360 Points

Can anyone explain how $product_id works?

I have been wondering from the beginning of it being introduced ($product_id) if it is a build-in php function (since $product_id is never explicitly defined in our project) and how it knows that it should refer exactly to $product[101] and not for example $product[0].

$product_id is just a variable that stores the keys from the key/value pair (product id and the product itself) The function basically loops through the products and pulls out the associated key.

Johnatan Guzman
Johnatan Guzman
Courses Plus Student 2,360 Points

Then why cant i change the variable name ($product_id) to something else without breaking it. Because we didnt explicitly specified it, it has to be a default php convention or in build function. Especially because, again, cant change the variable without breaking the loop.

Only thing I can think of is you'd have to change it everywhere it appears. Other than that I don't know, but searching for $product_id on php.net turns up no results which suggests it's not part of the base code.

2 Answers

Kevin Korte
Kevin Korte
28,111 Points

I'm pretty sure I familiarized myself with this project to answer this.

$product_id is NOT a built in function or variable of PHP. It was defined in the get_list_view_html function found in the products.php.

So with the get_list_view_html function, we can pass it two values. Those two values passed will be stored inside the function as $product_id and $product, respectively.

How are those values set?

Well we know that in products.php, we set a products array and filled with array items. So if you look at shirts.php, we include the products page, and thus the products array at the very top. We know have that array information of all shirts, and we have access now to the get_list_view_html function which also lives in the products.php file.

In shirts.php we run a foreach loop, that basically says take the entire $products array, and for each array in this array, set a key value pair where the key is the arrays index and name it $product_id, and the value is the array and name it $product.

Than inside of that for each loop, for each array item, we call the get_list_view_html() function, and pass it this particular loops $product_id, and $product array.

At this point the function gets evaluated, where the $product_id will be set in the $output string, and the details of the array like the img url and the name are also available.

When the function is done, we echo it to the screen, and move on to the next array, rinse and repeat.

It know to be $product[101] and not $product[0] because we defined the array's indexs as 101, 102, 103 in the products.php file

You could change $product_id to any valid PHP variable you want, you would just need to change that variable in the argument call in the get_list_view_html function, inside the function where $product_id gets evaluated, and in the foreach loop on shirts.php where we set $products_id => $products and also just below where the function gets echoed with again the $products_id variable as an argument.

That's a lot, but I hope that makes sense.

Johnatan Guzman
Johnatan Guzman
Courses Plus Student 2,360 Points

Wow, thats what I call an answer! I understand it a lot better now. Is it true that the $products array now has 101 undefined values (index 0-100) like it would be in javascript?

Kevin Korte
Kevin Korte
28,111 Points

Hey thank you.

No, in PHP the array will start with 101 instead of 0. The array has exactly what you see in the code. You yourself can see this by putting this line print_r($products); and the very end of the products.php file, after the products array. Than navigate to the product.php file in your browser, and you should see the following output.

Array ( [101] => Array ( [name] => Logo Shirt, Red [img] => img/shirts/shirt-101.jpg [price] => 18 [paypal] => 9P7DLECFD4LKE [sizes] => Array ( [0] => Small [1] => Medium [2] => Large [3] => X-Large ) ) [102] => Array ( [name] => Mike the Frog Shirt, Black [img] => img/shirts/shirt-102.jpg [price] => 20 [paypal] => SXKPTHN2EES3J [sizes] => Array ( [0] => Small [1] => Medium [2] => Large [3] => X-Large ) ) [103] => Array ( [name] => Mike the Frog Shirt, Blue [img] => img/shirts/shirt-103.jpg [price] => 20 [paypal] => 7T8LK5WXT5Q9J [sizes] => Array ( [0] => Small [1] => Medium [2] => Large [3] => X-Large ) ) [104] => Array ( [name] => Logo Shirt, Green [img] => img/shirts/shirt-104.jpg [price] => 18 [paypal] => YKVL5F87E8PCS [sizes] => Array ( [0] => Small [1] => Medium [2] => Large [3] => X-Large ) ) [105] => Array ( [name] => Mike the Frog Shirt, Yellow [img] => img/shirts/shirt-105.jpg [price] => 25 [paypal] => 4CLP2SCVYM288 [sizes] => Array ( [0] => Small [1] => Medium [2] => Large [3] => X-Large ) ) [106] => Array ( [name] => Logo Shirt, Gray [img] => img/shirts/shirt-106.jpg [price] => 20 [paypal] => TNAZ2RGYYJ396 [sizes] => Array ( [0] => Small [1] => Medium [2] => Large [3] => X-Large ) ) [107] => Array ( [name] => Logo Shirt, Teal [img] => img/shirts/shirt-107.jpg [price] => 20 [paypal] => S5FMPJN6Y2C32 [sizes] => Array ( [0] => Small [1] => Medium [2] => Large [3] => X-Large ) ) [108] => Array ( [name] => Mike the Frog Shirt, Orange [img] => img/shirts/shirt-108.jpg [price] => 25 [paypal] => JMFK7P7VEHS44 [sizes] => Array ( [0] => Large [1] => X-Large ) ) ) 
Leigh Maher
Leigh Maher
21,830 Points

Hi Kevin, at the beginning you state that $product_id was defined in the get_list_view_html. Are you sure that's right. Isn't that just a working variable, and doesn't the actual variable $product_id get defined in shirt. php i.e. $product_id = $_GET['id'];

It seems to me that it just happens that he uses the same name for the working variable in the function. This does seem logical but it actually confused me because I didn't know if I was working with the actual variable in products.php or the working variable.

Is this a common practice: to use the same names for the actual variable and the working variable. Again, it does seem like a logical thing to do but it did cause me some confusion.

Kevin Korte
Kevin Korte
28,111 Points

Well, products.php is included first in shirt.php :) But seriously you are correct, and I didn't notice that. There is where $product_id get's it's initial value. It gets passes as an argument to that function. I should have seen that.

You are correct with your assumptions. The working variable is only available inside the function, and any modification to it in the function doesn't reflect what was set by the $_GET value, unless the function's job is to set the new value. It seems more or less common to use the same variable for the argument. You can change that if it helps you though by reassigning it to a new variable value to work with inside the function.

Good catch and thank you

Leigh Maher
Leigh Maher
21,830 Points

Great. Thanks for clearing that up, Kevin. It all makes sense to me now : )