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:
If so, how would I be able to get each return separately? |
|||||||||
|
There is no way of returning 2 variables. Although you CAN propagate an array and return it; create a conditional to return a dynamic variable, etc. For instance, this function would return
In application:
If you wanted them both, you could modify the function a bit
|
|||||
|
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
Technically, you're returning an array and using The
This technique is also used in some functions defined by php itself (e.g. 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
It should be noted that while Finally, I would like to mention that
However, the following will do something different:
If you used |
|||||||||||||||||
|
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:
|
|||||||||||||
|
Or you can pass by reference:
This would output
|
|||
|
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. |
|||
|
In PHP 5.5 there is also a new concept:
|
|||
|
yes, you can use an object :-) but the simplest way is to return an array:
|
|||
|
yes and no, you can't return more than one variable / object, but as you suggest, you can put them into an array and return that, there is no limit to the nesting of arrays so you can just package them up that way to return |
|||
|
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. |
|||
|
Thought I would expand on a few of the responses from above....
This will give you a input field and a submit button once submitted, if the name input field is empty it will return the error flag and a message. If the name field has a value it will return the value/name and a error flag of 0 for false = no errors. Hope this helps! |
|||
|
You can return multiple arrays and scalars from a function
|
||||
|
The answer is no. When the parser reaches the first return statement, it will direct control back to the calling function - your second return statement will never be executed. |
|||
|
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. |
|||
|
Languages which allow multiple returns usually just convert the multiple values into a data structure. For example, in python you can return multiple values, however they're actually just being returned as 1 tuple. So you can return multiple values in PHP by just creating a simple array and returning that. |
|||
|
You can get values of two or more variables by setting them by reference
output:
|
|||
|
I think eliego has explained the answer clearly. But if you want to return both values, put them into a array and return it.
//to access return values
|
|||
|
I had a similar problem - so I tried around and googled a bit (finding this thread). After 5 minutes of try and error I found out that you can simply use "AND" to return two (maybe more - not tested yet) in one line of return. My code:
it works. I got both the values I expected to get/should get. I hope I could help anybody reading this thread :) |
|||
|
I have implement like this for multiple return value PHP function. be nice with your code. thank you.
|
||||
|
function test($testvar="") { // do something if($testvar) { return $var1="var1"; } else { return $var2="var2"; } } echo test(); |
|||||
|