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 PHP Functions Introducing Functions PHP Function Arguments

Lolo Handricks
Lolo Handricks
Front End Web Development Techdegree Student 10,706 Points

$arr, $name, $names

I understand how $arr and $name work together, but how does the function know that the $names variable goes together with the $arr and $name variables when it calls the function using the $names variable in the ()?

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Eloise,

As Hampton mentioned this is called passing by value, essentially what this means is we're passing a copy of the array $names to our hello function which when executed becomes $arr as it's now a scoped parameter instead of a global variable. Now that the function has a copy of the array we iterate over it using the foreach function as shown in the video.

Hope that helps.

adrien kovanic
adrien kovanic
5,727 Points

hi, I had the same question, to go a little deeper, I don't understand why it is selecting this array 'names' how come that if I add on other array under let's say last_name it will not call it in the function?

thanks a lot

Anna Donlin
Anna Donlin
687 Points

Hello, In layman's terms: the code on line 4 of the lesson if(is_array($arr)){ applies the name of the array $names to $arr. I did the lesson, renaming the $names array to $gypsies just to clarify it to myself. It worked. The foreach($arr as $name) statement on line 5 applies each piece of the array to the $name variable. It's circular, since the array is defined after the function. It's not intuitive to then associate the array back to the is_array line in the function.

Hope this helps!