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