Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
A sometimes useful concept inside of PHP is that of variable functions. This is a way for you to use a variable's value to call a function of the same name.
-
0:00
A sometimes useful concept inside of PHP is that of variable functions.
-
0:06
This is a way for you to use a variable's value to call a function of the same name.
-
0:11
This way if a variable name has parenthesis appended to it, PHP will look
-
0:16
for a function with the same name as whatever the variable evaluates to,
-
0:20
and then will attempt to execute it.
-
0:23
We will use this mostly as a callback in our code,
-
0:25
allowing us to call different functions from another function or
-
0:28
from an action based off of the result of the main function.
-
0:32
Let's go to Workspaces and look at this a bit closer.
-
0:36
>> Okay. So, let's open up some php tags here and
-
0:39
we have existing functions already for hello and greet.
-
0:43
Well now, let's say function is equal to hello.
-
0:47
So, this is just simple, variable named func, F-U-N-C, or function.
-
0:52
And then, we set it equal to hello.
-
0:54
Now, this variable can be named anything, just like a normal string variable.
-
0:58
You could name it, you know, Bob, Chris, Tom, but it makes sense that
-
1:02
we're going to use a function name, so we'll just say, func equals hello.
-
1:08
Now, in order to call it we simply use $func, but then open and
-
1:12
close parentheses, and put a semi colon after it.
-
1:16
This is the exact same as if we called hello with open and
-
1:19
close parentheses, because in variable functions inside of
-
1:23
php whatever the value of the variable name is.
-
1:27
Which the value is hello of func, it will call that function, or
-
1:31
look for a function with that same name.
-
1:35
So, let's say we change func to greet, and then called F-U-N-C, or
-
1:39
func with the open and close parentheses.
-
1:42
It would be the same as if we had called greet.
-
1:45
We can also pass through arguments such as dollar sign func and then Mike and
-
1:50
then evening.
-
1:51
This would be the exact same as if we called greet then Mike
-
1:55
then comma then evening.
-
1:58
Okay. So, where we left off we actually have
-
2:01
a function here called add up.
-
2:03
Let's say, we'll call it add up and then we'll have another one and
-
2:07
we'll call it subtract.
-
2:09
So, first thing we're gonna do is just simplify these just to make them nice and
-
2:13
clean for now.
-
2:15
We'll actually return just $a plus $b.
-
2:19
Okay.
-
2:20
Save that, and then we want to add up those two values, which is totally fine.
-
2:27
And then, we'll add another function in here.
-
2:31
And, we'll call it, answer.
-
2:36
Okay? And then,
-
2:37
that answer won't have any arguments, but we will create it none the less.
-
2:43
And we'll say, return, and then we will return the integer 42.
-
2:47
Okay.
-
2:49
So now, we have our return value of 42 from the answer function and
-
2:54
then we add up two values.
-
2:56
And then here, we have us calling add_up.
-
2:58
We're actually gonna remove this for right now.
-
3:01
Okay. So,
-
3:01
to talk about variable functions, we have two functions here.
-
3:05
The idea behind a variable function is that we're actually able to
-
3:10
call a function by using a string variable.
-
3:13
So, let's try this by creating a new variable, a string variable.
-
3:17
And, we'll call it func, F-U-N-C.
-
3:20
We'll set that equal to a string called answer.
-
3:24
All right.
-
3:26
So, nothing more than just a string variable.
-
3:29
But, in php, if we wanna call maybe a function called answer,
-
3:33
we can do that by simply saying func, which is our variable and
-
3:38
then adding a open and close parenthesis around it, and then doing a semi colon.
-
3:43
So, I'm gonna save this and then go over to our preview, and see what we get.
-
3:49
Okay. We got nothing back.
-
3:51
The reason why, is we didn't actually echo anything.
-
3:53
So, let's actually do echo of func.
-
3:57
Head back over after saving it and refresh.
-
4:00
Perfect, 42. We can actually store this to another
-
4:03
variable if you want.
-
4:04
So, we'll just call it num is equal to funk and then say, echo num.
-
4:12
Save it, go back over, and Refresh.
-
4:15
And, we get the same thing, 42.
-
4:16
We just went about it a different way.
-
4:18
All right.
-
4:19
So, that's a base of any kind of variable function.
-
4:24
But, let's say we wanna change this easily without modifying it at all.
-
4:28
We'll just say, change our string variable value to add up.
-
4:33
Now, this unto itself won't do us much good.
-
4:36
We refresh it and we get 0.
-
4:37
Well, that's because adding 0 and 0 we get 0.
-
4:41
So, we can actually pass through arguments right here on line 13.
-
4:45
So, we can say 5 and then 10.
-
4:49
Save that, head back over, and refresh.
-
4:53
And, we'll get 15.
-
4:54
So, what you can see here with variable functions is that it
-
4:58
as allows us to take input from somewhere and
-
5:02
call any kind of function that preexists, based off of a string variable value.
-
5:08
This could be very helpful for callbacks, which we'll get into in later lessons.
-
5:12
But for now, you can see that just simply taking a variable value, such as add up or
-
5:17
answer, and putting parents behind that function, or
-
5:21
behind that variable definition, you can actually call a variable function.
You need to sign up for Treehouse in order to download course files.
Sign up