Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Sorting Array Items

Where comes from the $item variable?

In the videos, I see the variable $item but I don't see it declared anywhere. Why so?

1 Answer

Kevin Korte
Kevin Korte
28,148 Points

I'm not familiar with the lesson, but I peaked at the code.

The only spot I can find $item is in functions.php. $item is used in both functions, the get_item_html function, and the array_category function. In both cases, $item is defined as an argument, and is only available inside that function, it's not a global variable.

In the case of the get_item_html function, when that gets called in catalog.php, we are passing in two variables as arguments, $id and $catalog[$id]. Those arguments are passed into get_item_html, and are referenced as $id and $item inside of the function.

Similar with array_category function. When we run the foreach loop, we are taking the $catalog and breaking it out into a key => value pair with temporary variables, $id and $item. These two variables are only available inside of this foreach function.