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

Trevor Jones
Trevor Jones
1,962 Points

Please help me understand the logic of "as $id => $item" when declaring our function in foreach loop.

It was around the 3 minute mark of the video "Creating the Display Function." She is doing this so that we get our item id, or key. I understand the purpose.

I'm trying to understand the logic. What exactly does "$id => $item" do that allows it to run the item's key as well as the value of $item? She doesn't really say what the code is doing, she's more saying what it will do.

Thank you

4 Answers

I might be misunderstanding what you're asking, but I'll give it a shot. (I am also not someone who knows exactly all the behind the scenes, so this is my best guesswork).

$arr = array ("key1" => "value1", "key2" => "value2");
foreach($arr as $key=>$value) {
echo $key;
echo $value;

Looking above, PHP runs through the code, grabs the array, then starts the loop. PHP sees the associative, or => symbol, between the two variables, which it then places the pairing as the key=>value.

It does it this way because that's what the foreach loop expects, either just the value, or the key=>value pairing.

Jerry Schrader
Jerry Schrader
4,396 Points

SO in other words, you are saying that ($id, $item) is the same as (key, value) ?

Robert Leonardi
Robert Leonardi
17,151 Points

You can rename to anything foreach($catalog as $key => $value) and foreach($catalog as $mySpecialKey => $mySuperSpecialValue) are both the same... just make sure within that foreach, you are using the same variable name you created accordingly.

In this video , she basically separating the display function so that it can be used in different/multiple pages.

What the function does is:

  1. Taking the $id and $value as the arguments
  2. Runs these arguments inside its function (the foreach)
  3. Stores it in $output
  4. Return $output

FYI, step 3 is actually NOT necessary. You can directly return "<td>..... </td><td>..... </td><td>..... </td>";

Hi Trevor,

I had trouble understanding and posted a question on this as well. In as simple terms as possible, $id is the key and $item is the value. For me this is confusing, but it is what was used in in the lesson.

I would have written it differently like this....

foreach($catalog as $key => $value) {
     // some code here;