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

Confused about this example

I am confused, as the argument the array is named, $user_array, why when the array is declared inside the function the array is called $name instead of being called $user_array like how it was in the argument at the beginning?

<?php function welcome_dialog($user_array) { if(is_array($user_array)) { foreach($user_array as $name) { echo "Hello, $name, how are you?</br>"; } } else { echo 'Hello friends</br>'; } }

$name = array(
    'Kcin',
    'Sharon',
    'Brother',
    'Sister'
);

welcome_dialog($name);

?>

2 Answers

David Olson
David Olson
8,690 Points

The $name array is actually set outside of your function. It is then passed into the function by adding the line welcome_dialog($name);

The $user_array argument is just a placeholder. By passing in $name it takes the place in all locations within the function where $user_array is used.

I will to show for you some example, let's go , firts :

//create a function function hello($arr){ if(is_array($arr)){ //using a if statement to compare if is array the $arr foreach($arr as $name){ //for each element $arr as $name, now $name is $arr ..
echo "Hello $name , how it's going!<br >\n"; } }else{ echo 'Hello friends!'; } }

$names = array( //we have create an array 'Rafael', 'Mike', 'macedo' );

hello($names); //$names is an argument inside of hello , $names is equal to $arr and $arr is equal to $name all names are inside of an array now belong to $name , I hope help you ...