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 PHP Internal Functions PHP Array Functions

Siraj Khan
Siraj Khan
3,451 Points

Why does `function print_info($key,$value){ echo "$key is a $value.<br>"; }` print : "Frog is Mike" .??

Why does function print_info($key,$value){ echo "$key is a $value.<br>"; } prints :

"Frog is a Mike" .??

AND

function print_info($value, $key){ echo "$key is a $value.<br>"; } prints :

"Mike is a Frog" .??

Please help.

2 Answers

Steven Parker
Steven Parker
231,269 Points

The order of the parameters in the function definition makes the difference:

// the example that prints "Frog is a Mike":
function print_info($key, $value)              // <-- note $key comes first
// the example that prints "Mike is a Frog":
function print_info($value, $key)              // <-- but here $value comes first
Siraj Khan
Siraj Khan
3,451 Points

But as shown in the lesson, Mike is the $key and Frog is the $value i.e

  'Mike' => 'Frog' ;

so that way

function print_info($key, $value)

should print "Mike is a Frog".!

Can you please a enlighten me in a bit detail.?

Steven Parker
Steven Parker
231,269 Points

But here, $key and $value are just parameter names. When "array_walk" calls the function, it will always put the value first and then the key. So if you want the parameter names to fit with what the arguments will be, $value must be the first one.

Siraj Khan
Siraj Khan
3,451 Points

But then why do we echo "$key is a $value."; Shouldn't the preview be according to echo..????

Steven Parker
Steven Parker
231,269 Points

It is, but remember that "$key" is just a parameter name, it's not actually the key unless it is also the second argument in the function definition. Don't let the names confuse you, they could be anything. For example, this would also work:

function print_info($one, $two) {
    echo "$two is a $one.<br>";
}