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.


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?


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