Returning Values7:51 with Hampton Paulk
In most cases we will want to do more than just echo a value inside of our functions. In order to do that we will need to have the function 'return' a value. This way we are able to store data from a function call to any type of value variable.
[MUSIC] 0:00 In most cases, we'll wanna do more than just echo a value inside of our functions. 0:04 In order to do that, we'll need to have the function return a value. 0:10 This way, we're able to store data from a function call, 0:14 to any type of value variable. 0:17 Those can include arrays, integers, Booleans, so on and so forth. 0:20 A return statement will also end the function's execution immediately. 0:24 This way, we can pass control back to the line that the function was called from. 0:29 Allowing us to return at different locations inside our function. 0:33 Although a function cannot return multiple values, it can however return an array, 0:38 containing the multiple values inside of a single data type. 0:43 Let's jump over to workspaces, and work with the return statement. 0:48 >> So as we were talking about earlier, 0:52 we're gonna wanna do more than just echo values inside of our functions. 0:54 We're gonna wanna return values. 0:58 So there is a keyword in PHP called return, 1:00 that we can use inside of our functions. 1:04 There're a lot of great uses of return, 1:07 more than just returning the value of whatever we want. 1:09 So, let's create a couple of small functions to just sort of 1:13 see what we can do. 1:17 So to get started, let's create a very simple function called Hello, 1:19 just like before. 1:22 [BLANK_AUDIO] 1:23 Okay? 1:25 And then open and 1:26 close our curly braces to give ourselves function boundaries there. 1:27 And then inside, we're going to return a value. 1:30 All right, so let's return the same kind of string we would before, 1:33 like Hello World. 1:38 Now the way we're gonna do that, instead of saying echo, we'll say return. 1:40 All right? And then we'll say return, and 1:45 then do our typical string here. 1:48 Hello World. 1:50 All right? Close it with a semicolon, and now when we 1:54 call the function, when we run just Hello, it won't really do us any good. 1:57 We need to store this somewhere. 2:03 So the output of this needs to do something. 2:04 So we'll store it to a variable called Greeting. 2:07 [BLANK_AUDIO] 2:10 Greeting is equal to the output or 2:12 the return value on line four of the function Hello. 2:15 So, we'll go down here now, and if we save this and 2:19 run our preview, let's see what it does. 2:21 [BLANK_AUDIO] 2:24 Okay? You'll see we actually have no output. 2:25 The reason why, is we're not echoing. 2:28 We're just returning. 2:29 But if we say echo and then greeting, all right? 2:30 And then hit Save, go back over and refresh. 2:36 Now it'll say hello world. 2:40 It's actually outputting that variable, which the variable is just a string, but 2:42 the string was generated by our function, which you can see on line seven. 2:46 Okay, let's do a little bit more with this. 2:52 So instead of just saying hello, we're gonna pass on an argument here. 2:54 We're gonna say name. 2:57 All right. 2:59 And then, we're gonna do a couple of, of tests in here. 3:00 So we'll say if and then open and close, name. 3:04 [BLANK_AUDIO] 3:09 Okay? 3:10 Equals Mike, right? 3:11 Then what are we gonna do? 3:14 We will actually go here and 3:16 we'll return instead of Hello World, we'll say Hello Mike. 3:17 [BLANK_AUDIO] 3:22 All right? Make sure we do our curly braces here. 3:24 All right, all our tabs are good. 3:27 So that will be pretty good as long as it's equal to Mike. 3:29 Else we wanna do something different. 3:33 Otherwise, so we'll say else 3:35 we'll just say return not echo, hello stranger. 3:40 [BLANK_AUDIO] 3:46 Doesn't need to be caps. 3:48 All right. Close our single quotes and 3:51 then a semicolon. 3:53 And so now, when we pass through name and say it's Mike. 3:54 We should get still an echoed greeting. 4:00 So let's go back over, hit Refresh, except we're gonna get, Hello Mike. 4:01 Now if we were to change it to something else. 4:06 So Chris, hit Save, head back over and 4:08 then hit Refresh, now it says, hello stranger. 4:11 Okay, so you can see here we're returning a value here, in a conditional. 4:16 Just the same way we were before. 4:20 So we can have multiple return values, but 4:22 we cannot return more than one value from a single function. 4:25 So it only has one return clause. 4:29 So once it runs the return, it exits the function. 4:31 Let's say we wanna do something with an integer calculation. 4:36 So what we'll do is we'll say sum and 4:39 then we'll do A and B as our two variables. 4:44 Actually that's not against sum 'cuz that is an internal word. 4:48 We'll say add up. 4:51 Let's say that's our function. 4:54 Okay, so now, I'm gonna get rid of this, and 4:56 then we're going to say our value is equal to add up. 5:00 And we are going to add up two numbers. 5:07 So the first argument would be two, the second argument would 5:09 be the integer four, and then we will echo our value. 5:14 All right. 5:19 So that won't do anything cuz we haven't returned anything. 5:19 So we need to do a return. 5:22 [UNKNOWN] Return and then we'll do $A, plus $B. 5:23 Close it with a semi colon, and then go look at our preview. 5:30 Six. 5:35 Exactly what we were looking for. 5:35 Okay, so that's a way we can return integer values, we can return strings. 5:37 If we need to return multiple values, we can. 5:42 But we need to do it with an array. 5:46 So let's say we wanna actually return the two values that came in, 5:48 as well as the summed value. 5:53 So, the way we'll do that, is we're gonna create an internal array, 5:55 we'll just call it ARR. 5:59 And we will set it equal to let's see, the first value would be, 6:01 let's see, array, go down, keep it tidy. 6:08 All right, so the first value is going to be $a, okay. 6:12 The next one would be $b, which gives us our two input values, and 6:17 then finally we'd need our summed value, which would be A plus B. 6:20 So we can just simply say A plus $B. 6:24 Okay, so we have three different values here in our array. 6:29 The first one is $A. 6:32 The second one is $b. 6:34 And the third one would be $a plus $b. 6:35 And then to return instead of returning a plus b, we'll just simply return ARR. 6:39 Now, if I was to just echo this value, let's see what we get back. 6:45 [BLANK_AUDIO] 6:49 Okay, we get a string array. 6:51 Well, we can use an internal function of PHP, which we'll get into a little bit 6:53 more on internal functions, but we have one that's called print R. 6:58 So I'll just say instead of echo, print R, and then pass through the value. 7:02 That way we'll actually get to see the keys and values of the array. 7:09 And back over to our preview and hit Refresh. 7:13 So here you can see that key zero for the array is the two, which is our A. 7:16 Key one would be four, and then our output value, or key two, would be six. 7:21 So if we wanted to, we wanted to instead of print up just the whole array, 7:28 we would just say echo, and then value. 7:33 And then the final key, right? 7:36 Which would be two. 7:39 All right, so we'll semi-colon there, end our statement, and then refresh. 7:40 So now we can get back our value, but 7:44 we also have the a and the b as well, if we needed to use it. 7:46
You need to sign up for Treehouse in order to download course files.Sign up