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 Creating the Display Function

yalın küçük
yalın küçük
4,810 Points

Do we have to put a variable ( argument) in function we do not need or use within? I do not understand.

Hi,

https://teamtreehouse.com/library/build-a-basic-php-website/listing-and-sorting-inventory-items/creating-the-display-function

You remember I guess.

We do not use the $id variable here. No statement at all. There is not any reference of it.

function get_item_html($id, $item){ $output = "<li><a href='#'><img src='". $item["img"] . " ' alt=' " . $item["title"] . " ' />". "<p>View Details</p>" . "</a></li>"; return $output;


Here below we have but $id is not being used at all.

foreach($catalog as $id => $item){ echo get_item_html($id, $item);

}

You can see below, without $id function and foreach will work.

Function get_item_html($item){ $output = "<li><a href='#'><img src='". $item["img"] . " ' alt=' " . $item["title"] . " ' />". "<p>View Details</p>" . "</a></li>"; return $output;

foreach($catalog as $id => $item){ echo get_item_html($item); }

Why did she put that? What is the point?

Thanks

Steven Snary
Steven Snary
17,540 Points

The $id s not used in the get_item_html() function - you are correct. In fact if you take the $id out from the parameter list in the function header and do not pass $id into the function call the code will still execute correctly.

$id is still required in the foreach expression since $catalog is an associative array and we want the foreach loop to separate the key and value from the $catalog array.

Maybe the key will be used in an upcoming lesson???