1 00:00:00,890 --> 00:00:06,100 A sometimes useful concept inside of PHP is that of variable functions. 2 00:00:06,100 --> 00:00:11,510 This is a way for you to use a variable's value to call a function of the same name. 3 00:00:11,510 --> 00:00:16,150 This way if a variable name has parenthesis appended to it, PHP will look 4 00:00:16,150 --> 00:00:20,600 for a function with the same name as whatever the variable evaluates to, 5 00:00:20,600 --> 00:00:23,120 and then will attempt to execute it. 6 00:00:23,120 --> 00:00:25,850 We will use this mostly as a callback in our code, 7 00:00:25,850 --> 00:00:28,960 allowing us to call different functions from another function or 8 00:00:28,960 --> 00:00:32,450 from an action based off of the result of the main function. 9 00:00:32,450 --> 00:00:35,060 Let's go to Workspaces and look at this a bit closer. 10 00:00:36,220 --> 00:00:39,070 >> Okay. So, let's open up some php tags here and 11 00:00:39,070 --> 00:00:43,860 we have existing functions already for hello and greet. 12 00:00:43,860 --> 00:00:47,870 Well now, let's say function is equal to hello. 13 00:00:47,870 --> 00:00:52,398 So, this is just simple, variable named func, F-U-N-C, or function. 14 00:00:52,398 --> 00:00:54,750 And then, we set it equal to hello. 15 00:00:54,750 --> 00:00:58,770 Now, this variable can be named anything, just like a normal string variable. 16 00:00:58,770 --> 00:01:02,190 You could name it, you know, Bob, Chris, Tom, but it makes sense that 17 00:01:02,190 --> 00:01:07,040 we're going to use a function name, so we'll just say, func equals hello. 18 00:01:08,250 --> 00:01:12,440 Now, in order to call it we simply use $func, but then open and 19 00:01:12,440 --> 00:01:14,840 close parentheses, and put a semi colon after it. 20 00:01:16,080 --> 00:01:19,400 This is the exact same as if we called hello with open and 21 00:01:19,400 --> 00:01:23,260 close parentheses, because in variable functions inside of 22 00:01:23,260 --> 00:01:27,430 php whatever the value of the variable name is. 23 00:01:27,430 --> 00:01:31,720 Which the value is hello of func, it will call that function, or 24 00:01:31,720 --> 00:01:33,740 look for a function with that same name. 25 00:01:35,160 --> 00:01:39,630 So, let's say we change func to greet, and then called F-U-N-C, or 26 00:01:39,630 --> 00:01:42,810 func with the open and close parentheses. 27 00:01:42,810 --> 00:01:45,840 It would be the same as if we had called greet. 28 00:01:45,840 --> 00:01:50,590 We can also pass through arguments such as dollar sign func and then Mike and 29 00:01:50,590 --> 00:01:51,760 then evening. 30 00:01:51,760 --> 00:01:55,590 This would be the exact same as if we called greet then Mike 31 00:01:55,590 --> 00:01:56,980 then comma then evening. 32 00:01:58,140 --> 00:02:01,300 Okay. So, where we left off we actually have 33 00:02:01,300 --> 00:02:03,495 a function here called add up. 34 00:02:03,495 --> 00:02:07,760 Let's say, we'll call it add up and then we'll have another one and 35 00:02:07,760 --> 00:02:09,480 we'll call it subtract. 36 00:02:09,480 --> 00:02:13,420 So, first thing we're gonna do is just simplify these just to make them nice and 37 00:02:13,420 --> 00:02:14,220 clean for now. 38 00:02:15,570 --> 00:02:19,638 We'll actually return just $a plus $b. 39 00:02:19,638 --> 00:02:20,925 Okay. 40 00:02:20,925 --> 00:02:27,770 Save that, and then we want to add up those two values, which is totally fine. 41 00:02:27,770 --> 00:02:31,260 And then, we'll add another function in here. 42 00:02:31,260 --> 00:02:34,890 And, we'll call it, answer. 43 00:02:36,570 --> 00:02:37,310 Okay? And then, 44 00:02:37,310 --> 00:02:43,610 that answer won't have any arguments, but we will create it none the less. 45 00:02:43,610 --> 00:02:47,270 And we'll say, return, and then we will return the integer 42. 46 00:02:47,270 --> 00:02:47,770 Okay. 47 00:02:49,640 --> 00:02:54,080 So now, we have our return value of 42 from the answer function and 48 00:02:54,080 --> 00:02:56,460 then we add up two values. 49 00:02:56,460 --> 00:02:58,700 And then here, we have us calling add_up. 50 00:02:58,700 --> 00:03:01,320 We're actually gonna remove this for right now. 51 00:03:01,320 --> 00:03:01,863 Okay. So, 52 00:03:01,863 --> 00:03:05,860 to talk about variable functions, we have two functions here. 53 00:03:05,860 --> 00:03:10,360 The idea behind a variable function is that we're actually able to 54 00:03:10,360 --> 00:03:13,930 call a function by using a string variable. 55 00:03:13,930 --> 00:03:17,720 So, let's try this by creating a new variable, a string variable. 56 00:03:17,720 --> 00:03:20,400 And, we'll call it func, F-U-N-C. 57 00:03:20,400 --> 00:03:24,112 We'll set that equal to a string called answer. 58 00:03:24,112 --> 00:03:26,500 All right. 59 00:03:26,500 --> 00:03:29,070 So, nothing more than just a string variable. 60 00:03:29,070 --> 00:03:33,900 But, in php, if we wanna call maybe a function called answer, 61 00:03:33,900 --> 00:03:38,650 we can do that by simply saying func, which is our variable and 62 00:03:38,650 --> 00:03:43,010 then adding a open and close parenthesis around it, and then doing a semi colon. 63 00:03:43,010 --> 00:03:47,560 So, I'm gonna save this and then go over to our preview, and see what we get. 64 00:03:49,720 --> 00:03:51,110 Okay. We got nothing back. 65 00:03:51,110 --> 00:03:53,240 The reason why, is we didn't actually echo anything. 66 00:03:53,240 --> 00:03:56,075 So, let's actually do echo of func. 67 00:03:57,150 --> 00:03:59,540 Head back over after saving it and refresh. 68 00:04:00,600 --> 00:04:03,520 Perfect, 42. We can actually store this to another 69 00:04:03,520 --> 00:04:04,560 variable if you want. 70 00:04:04,560 --> 00:04:10,630 So, we'll just call it num is equal to funk and then say, echo num. 71 00:04:12,160 --> 00:04:15,010 Save it, go back over, and Refresh. 72 00:04:15,010 --> 00:04:16,690 And, we get the same thing, 42. 73 00:04:16,690 --> 00:04:18,120 We just went about it a different way. 74 00:04:18,120 --> 00:04:19,500 All right. 75 00:04:19,500 --> 00:04:24,560 So, that's a base of any kind of variable function. 76 00:04:24,560 --> 00:04:28,470 But, let's say we wanna change this easily without modifying it at all. 77 00:04:28,470 --> 00:04:32,130 We'll just say, change our string variable value to add up. 78 00:04:33,680 --> 00:04:36,220 Now, this unto itself won't do us much good. 79 00:04:36,220 --> 00:04:37,750 We refresh it and we get 0. 80 00:04:37,750 --> 00:04:41,120 Well, that's because adding 0 and 0 we get 0. 81 00:04:41,120 --> 00:04:45,260 So, we can actually pass through arguments right here on line 13. 82 00:04:45,260 --> 00:04:49,720 So, we can say 5 and then 10. 83 00:04:49,720 --> 00:04:52,500 Save that, head back over, and refresh. 84 00:04:53,840 --> 00:04:54,400 And, we'll get 15. 85 00:04:54,400 --> 00:04:58,520 So, what you can see here with variable functions is that it 86 00:04:58,520 --> 00:05:02,520 as allows us to take input from somewhere and 87 00:05:02,520 --> 00:05:08,320 call any kind of function that preexists, based off of a string variable value. 88 00:05:08,320 --> 00:05:12,040 This could be very helpful for callbacks, which we'll get into in later lessons. 89 00:05:12,040 --> 00:05:17,130 But for now, you can see that just simply taking a variable value, such as add up or 90 00:05:17,130 --> 00:05:21,160 answer, and putting parents behind that function, or 91 00:05:21,160 --> 00:05:24,950 behind that variable definition, you can actually call a variable function.