Re: returning multiple variables from a function?

From: Date: Mon, 12 Feb 2001 21:22:12 +0000
Subject: Re: returning multiple variables from a function?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-39265@lists.php.net to get a copy of this message
On Mon, Feb 12, 2001 at 09:13:09PM -0000, James, Yz wrote: > What if I wanted to "return" more than one variable from the function? And > how would I assign it a name? Like the actual variable, or is that just not > possible? > > I've tried > return $var1,$var2; You're on the right track. return Array($var1,$var2); And when you call the function, list($ret1,$ret2) = myfunc(); ..or.. $ret = myfunc(); ..and use $ret[0] and $ret[1] Another option is to pass variables by reference. For example, say you had a function that returned true or false, but you wanted it to return an error message with that false. Something like this: function test($condition,&$errormsg) { if ($condition == $rightanswer) { return true; } else { $errormsg = "The condition wasn't the right answer."; return false; } } Then to check it, if (!test('foo',$error)) echo "Failed test!<br>Error: $errormsg<br>"; HTH, Matt

« previous php.general (#39265) next »