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 Random Fun with Arrays

Why do we need 2 parameters in.... function get_item_html($id, $item){}

Why do we need the $id parameter?

The lesson works perfectly fine without it. Example below....

function get_item_html($item)

In the index.php file we don't even use the $id parameter from the include functions.php file.

By having the $id parameter this lesson confused me more. Is there an explanation I might be over-looking, such as more flexibility in the future?

Any help is appreciated. Thanks in advance!

3 Answers

The php array_rand() call in the index.php file returns an array of id (actually indices) from the catalog. Since those indices are the primary identifying factor for the values stored in the $catalog array we created in data.php, it's the safest "id" we can use to access the data. $item is defined, in fact, as $catalog[$id] in the get_item_html() call we use in index.php. You are half right, though, we could probably just pass in one value ($catalog[$id]) and get away with that, supposing we made the necessary changes to the get_item_html() function in functions.php. However, since we need the "id" to build the href of our $output later on anyway, it's just easier (and more efficient) to keep it separated as we handle it in our foreach loop.

We can restructure the code any number of ways, some more efficient, some easier to read. It's usually just a balancing act between performance, readability, and personal taste. Contrary to popular belief, there's no "right way" to do most things in programming aside from syntax, of course. If you can find a way to make it work that you understand more easily, it's often better to use that method. Usually, tutorials and examples are built around an approachable and tested way of doing something in the instructor's own personal tastes, which means it generally works if you do it exactly that way. That doesn't necessarily mean it's the "only way" to do it, nor the best.

Gotta say workspace took a dive on me when I tried to var_dump and wasted an entire night making sure I understood this, only hang up was the semantics behind the foreach = 321 problem, but I cannot repeat that at this point especially considering the unpredictable nature I've put up with with the workspace sadly. :( Tried using the downloadable resources to no avail, hopefully there is a workspace repo coming up soon. I have a kid and an angry pregnant woman leaving me every other day because I study all day everyday. Can someone look into the stability of the PHP workspaces in general?

Amirhossein Khademoulmeleh
Amirhossein Khademoulmeleh
2,941 Points

its pretty weird but i have the exact same situation, i have a kid and an angry pregnant woman :)) and i am also studying every night! so weird how stuff like this happen around the world!

Hi Jeffrey, I spent a whole evening trying to understand the result of the foreach = 321. The reason behind it is that in the foreach loop $output is assigned the value of the $id and $value variables concatenated together e.g.

$id = 3; $value = 21; $output = $id . $value;

So the above variable $output would equal 321. Remember the . operator joins items together.

and as the assignment of the $output variable is in the foreach loop each time the loop runs the previous value of $output is overwritten.

Hope this helps.

Just saying that I agree with you. As far as I can see this function only needs one parameter.