Is it possible to have a function with 2 returns like this:
function test($testvar)
{
// do something
return $var1;
return $var2;
}
If so, how would I be able to get each return separately?
Is it possible to have a function with 2 returns like this:
function test($testvar)
{
// do something
return $var1;
return $var2;
}
If so, how would I be able to get each return separately?
Does php still use "out parameters"? If so, you can use the syntax to modify one or more of the parameters going in to your function then, you would then be free to use the modified variable after your function returns.
Technically, you can't return more than one value. However, there are multiple ways to work around that limitation. The way that acts most like returning multiple values, is with the
list
keyword:Technically, you're returning an array and using
list
to store the elements of that array in different values instead of storing the actual array. Using this technique will make it feel most like returning multiple values.The
list
solution is a rather php-specific one. There are a few languages with similar structures, but more languages that don't. There's another way that's commonly used to "return" multiple values and it's available in just about every language (in one way or another). However, this method will look quite different so may need some getting used to.This technique is also used in some functions defined by php itself (e.g.
$count
in str_replace,$matches
in preg_match). This might feel quite different from returning multiple values, but it is worth at least knowing about.A third method is to use an object to hold the different values you need. This is more typing, so it's not used quite as often as the two methods above. It may make sense to use this, though, when using the same set of variables in a number of places (or of course, working in a language that doesn't support the above methods or allows you to do this without extra typing).
The above methods sum up the main ways of returning multiple values from a function. However, there are variations on these methods. The most interesting variations to look at, are those in which you are actually returning an array, simply because there's so much you can do with arrays in PHP.
First, we can simply return an array and not treat it as anything but an array:
The most interesting part about the code above is that the code inside the function is the same as in the very first example I provided; only the code calling the function changed. This means that it's up to the one calling the function how to treat the result the function returns.
Alternatively, one could use an associative array:
Php does have the
compact
function that allows you to do same as above but while writing less code. (Well, the sample won't have less code, but a real world application probably would.) However, I think the amount of typing saving is minimal and it makes the code harder to read, so I wouldn't do it myself. Nevertheless, here's a sample:It should be noted that while
compact
does have a counterpart inextract
that could be used in the calling code here, but since it's a bad idea to use it (especially for something as simple as this) I won't even give a sample for it. The problem is that it will do "magic" and create variables for you, while you can't see which variables are created without going to other parts of the code.Finally, I would like to mention that
list
doesn't really play well with associative array. The following will do what you expect:However, the following will do something different:
If you used
list
with an associative array, and someone else has to change the code in the called function in the future (which may happen just about any situation) it may suddenly break, so I would recommend against combininglist
with associative arrays.Functions in PHP can return only one variable. you could use variables with global scope, you can return array, or you can pass variable by reference to the function and than change value,.. but all of that will decrease readability of your code. I would suggest that you look into the classes.
You can always only return one variable which might be an array. But You can change global variables from inside the function. That is most of the time not very good style, but it works. In classes you usually change class varbiables from within functions without returning them.
In your example, the second return will never happen - the first return is the last thing PHP will run. If you need to return multiple values, return an array:
Functions, by definition, only return one value.
However, as you assumed, that value can be an array.
So you can certainly do something like:
That said, it's worth taking a moment and thinking about whatever you're trying to solve. While returning a complex result value (like an array, or an object) is perfectly valid, if you're thinking is that "I want to return two values", you might be designing poorly. Without more detail in your question, it's hard to say, but it never hurts to stop and think twice.