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 trialAmy Hsieh
6,023 PointsIs print_info an internal function of PHP or a self-defined function by user?
According to the PHP manual, print_info should be a user-defined function. But How can print_info know the two parameters ($value and $key) in that function? because we didn't even define them in that function?
Liam wylie
891 PointsThis is something I wondered, now I know this is 4 years ago and likely you've learned alot more but for others who haven't quite gotten their head around it;
(Correct me if I'm wrong also) In PHP its quite common to associate a key(in our workspace, a name) with a value(in our workspace, a title), in other programming languages you would make a less attached multi dimensional array such as
array names[][]
and you could use the names in the first section, and titles in the second section.
Now to the point. Knowing that assigning array keys a second 'value' is commonplace,
array_walk is structured such that if you were to perform a array_walk using a user defined function with 2 different parameters (in our case we did, with $keys and $values), it will assume the first one is the keys, and the second one is the values, because as I said before it's more commonplace than it is in other programming languages.
1 Answer
Joseph Cowan
6,231 PointsWhen the instructor does something like 'function print_info($key, $values)' he is essentially creating the function by using the "function" keyword AND defining what parameters/arguments need to be passed into that function by writing them in the declaration. When a function is being created (I think "declared" is the proper term), that is essentially where you first see what parameters are expected (if any).
so if I wrote something like this:
function doCoolStuff($thing){
echo "This is my cool thing: ".$thing;
}
I'm declaring the function, and what arguments/parameters it needs to work. The argument is pretty much just a placeholder for whatever you are trying to pass into the function.
So to actually USE the function I need to call it.
doCoolStuff('robot');
(this will then show the text "This is my cool thing: robot").
HOWEVER if I try to call it without an argument/parameters, it won't work.
doCoolStuff();
(this will cause it to crash)
If I did something like:
function doCoolStuff(){
echo "backflip!";
}
then when I call the function, I never declared the need for arguments/parameters to be passed into the function so that means calling it like this:
doCoolStuff();
will work.
Sorry I feel I might have over explained this. Short answer is, when he declared the function he also declared what is needed to be passed into it at the same time. Hope this helps!
Amy Hsieh
6,023 PointsAmy Hsieh
6,023 PointsYes, this helps me. Thank you so much.