Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the last movie, we learned how to return values from a function by using return. In this movie, we'll learn how to return multiple values from function. Well, sort of, you see a function can only return a single value. That's a hard and fast rule about functions, that it returns only a single value. So what if we need to return more than one value? Well first let's create an example so we can look at the problem more closely. I'm going to open up basic.html and I'll do a Save As on that and I'm going to call this Functions Multiple Returns. We'll give it the same name here.
Functions multiple returns... Alright let's start with a new function. And this function I've called add_subt. Short for subtract. So it's going to add and subtract two values. Value one and value two. We're going to add them together. And then we're going to subtract them. So we know how to use return and how to return add. But how can we also return subt two?. Let's just try this real quick and see. And let's go ahead and have result. It's going to be equal to add subt, let's do 10,5.
Right and then we'll echo back the result. All right, so, let's just try this and see what we get. Let's go to Firefox and instead of return values going to go to Multiple Returns.php. Alright. It gave me back 15. Now that's just the value of adding them together. It has nothing to do with subtracting. Now, you might try. Well, let's say, what if we had a comma? Maybe just do comma subt, save. Let's go back, reload the page. Nope, parse error. Unexpected comma. So it won't let us do that.
It's nothing that is that simple. The problem is that functions only allow us to return one thing. So that's it. We're limited to one thing that we can return. So let's think for a second, we have two things that we want to return. but a return will only let us pass back one thing. Is there a single PHP entity that will hold more than one entity? Yes, arrays. Arrays to the rescue. What we can do is return an array here instead. So array, and then add comma, and subtract.
There we go. Alright, we'll remove that comment now. Now it will return an array. So what we're getting back here for result is an array. Just to make that clear, this is result array. Now we don't want to echo the result array anymore, we probably want to do something a little nicer. So let's go to add. And then we'll concatenate that with result array zero to get the first value, that's the add. And then we'll put br tags at the end. And then let's do that same line again. Let's make the next one, though, subt.
We'll make it result array one. Alright, let's save it. Let's go back and try that. There we go. Add and subtract. We got both results back and we did it by using this array as the vehicle to get more than one value back, and that's great because arrays can return a lot of stuff, so we can return two values, three values, four values, its unlimited, we just have to wrap them up in this single structure, this single array, because that way that array can be our return value... Now, I don't know about you but I think that this feels a little bit clunky to me.
I really don't like the idea of having to write this in my code all the time, and this isn't very descriptive of what it does either. Fortunately, there's another, better way to do it. And that is to use another PHP function called list. You use list to assign array values to variables. So for example let's just copy all of this. And we'll drop down here, paste it in. This time, though, let's just change it add and subtract, we'll make it 20 and 7 and instead of result array, we're just going to say list and then in parentheses, provide the variables that we want to use. I'll call this one add result and I'll call this one subt result. Alright, so that's what it does.
It takes these values from the array and it assigns them to add result and subt result. Then here I can use those instead, a little less clunky. All right let's just try that out. Reload the page and there we go, 27 and 13. So I think list is super-handy because it allows us to take all those elements that we just packaged up into an array to get out of the function. And immediately break them back down, unpack it, and assign them two variables that have good, common sense names that are easier to identify than result_array.
The combination of the two using array. And list is the best way to return multiple values out of your functions.
Get unlimited access to all courses for just $25/month.Become a member