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

Raju Kolte
Raju Kolte
3,110 Points

confusing in arrguments

Hello team im new in php my question is what is the connection both of this $arr & $names when we call them we use $names but the first argument is $arr what they can connect each other... im confuse

4 Answers

$arr is a parameter. Its a placeholder in the function. At the end of the video $names is the array that is done like so:

<? php

$names = array(

the relationship of $names and $arr is that, $arr is a parameter for the function. Think of it as a variable for the function ONLY We then call the function with the $names variable like in the video. By calling the function with the $names variable we are able to echo the names above.

Presley Cobb
Presley Cobb
9,214 Points

If you could show the code you are talking about that would be great. But if the names of the variables are different then they are separate. You should not think of them as connected if they are declared normally. Normally meaning $var = "placeholder text";

Matthew Brock
Matthew Brock
16,791 Points
// the video uses $name first and expects it to be a string. 
function hello($name)

// then it shows using $arr as the variable for the function. 
function hello($arr)

// It then uses the if statement to check to see if it is an array.
if (is_array($arr)) {     // checks to see if is an array
  foreach($arr as $name) {          
     // this loops through every item in the array and gives you the value $name 
     // so first time through loop would be "Hampton"
     // then next time would be "Mike"
} else {              // is something other than array
Presley Cobb
Presley Cobb
9,214 Points

There is no difference in the way the variables are used. $arr and $name are used in the exact same way here. He just changed how he named the variable. Now what may be confusing you is that he creates an array called name and then passes the variable into the function. Basically just ignore the first example that where he creates a function called hello with $name as a parameter.

Focus on the second example. What he is doing is instead of passing a single value into the function he is passing an array of values. The name of that array is called $name. He should have used 3 different variable names because I can see how it would be confusing the way it was done.