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
yalın küçük
4,810 PointsDo we have to put a variable ( argument) in function we do not need or use within? I do not understand.
Hi,
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
17,540 PointsSteven Snary
17,540 PointsThe
$ids not used in theget_item_html()function - you are correct. In fact if you take the$idout from the parameter list in the function header and do not pass$idinto the function call the code will still execute correctly.$idis still required in theforeachexpression since$catalogis an associative array and we want theforeachloop to separate the key and value from the$catalogarray.Maybe the key will be used in an upcoming lesson???