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