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?
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 :)
You can return multiple arrays and scalars from a function
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!
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.