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 Displaying Categories

Alex Forseth
Alex Forseth
8,017 Points

At which point in the array_category function was the parameter $category defined?

As far as I understand it when we make a new function like

function array_category($catalog,$category) 

the $catalog and $category arguments need to be defined later in that same function. Or can we pass existing variables into the function arguments? Maybe a misunderstanding somewhere along the way in my learning but moving on.

Now I understand that the $catalog argument is associated with the $catalog array in data.php. Does this mean that the $catalog argument in the array_category function is now defined?

Also, since we have not defined $category ever. How and when does it get defined? If it isn't defined how does the foreach loop know what to look for? If it is has been defined please explain how because I don't understand how the foreach can possibly work.

if(strtolower($category) == strtolower($item["category"])) {
           $output[] =$id;
}

Please help.

1 Answer

andren
andren
28,558 Points

Parameters are essentially variables, so simply by typing them in the parenthesis you have defined them, the value that they are set to comes from the arguments that are passed to the function when it is called.

The array_category function is called at 3:48 in the video. The code that calls it looks like this:

$categories = array_category($catalog, 4);

The two arguments passed in the parenthesis are the data that is passed to the function. PHP will automatically take the first argument (The content of $catalog) and assign it to the first parameter of the function ($catalog) and takes the second argument (4) and assigns it to the second parameter ($category).

The argument to parameter pairing is done entirely based on the position of the argument/parameter (first argument to first parameter, second argument to second parameter, etc). So whether the names of the argument and parameter match or not is irrelevant.

If you are still confused then it might help to look at a simpler function. Let's take a very simple math function like this:

<?php

function add($a, $b) { // $a is parameter 1, $b is parameter 2
  return $a + $b;
}

add(5, 10); // 5 is argument 1, 10 is argument 2

That function simply takes two parameters and returns them added together. Inside the function $a will be set to 5 since that is the first argument that is passed to the function, and $b will be set to 10. So the function will return 15.

If you did define the parameters value yourself in the function, like this for example:

<?php

function add($a, $b) { // $a is parameter 1, $b is parameter 2
  $a = 0;
  $b = 0;
  return $a + $b;
}

add(5, 10); // 5 is argument 1, 10 is argument 2

Then the parameters would become useless, since their entire purpose is to allow you to pass in data when the function is called, but that data is now being overridden. The above function for example will always return 0 regardless of what arguments are passed in to it, which is obviously not very useful.