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 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???