Re: returning multiple variables from a function?

From: Date: Mon, 12 Feb 2001 17:36:07 +0000
Subject: Re: returning multiple variables from a function?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-39268@lists.php.net to get a copy of this message
An approach that many people take is to simply have a function return success or failure, and have any data that needs to be propogated upwards assigned to pass-by-reference parameters. I like this approach because it removes any question as to what a function returns. One drawback is that it becomes difficult to see where values are getting assigned when debugging. i.e.: function getMeTwoThings(&$first, &$second) { $first++; $second--; if ($first == $second) return true; else return false; } Alternatively, you can return an array of values and use the list() construct to assign them. i.e.: function foobar() { return array($a, $b); } list($a, $b) = foobar(); John On Mon, 12 Feb 2001, James, Yz wrote: > Hrms. Got a small problem. > > I'm now using functions on a regular basis, but I've come to another > sticking point with them: returning multiple variables from the function... > > Here's a really quick example (nothing to do with what I intend to use the > functions for): > > function CUP ($connection,$username,$password) { > > if (($username) && ($password)) { > $sql = "SELECT username, password FROM table > WHERE username = '$username' AND password = '$password'"; > $result = etc etc > $num = mysql_numrows($result); > > if ($num != 0) { > $valid = "yes"; > } > > return $valid; > > } > > > > $correct_user = CUP ($connection,$username,$password); > > Now, if the user is correct, I'd get a return of > > $correct_user = "yes"; > > 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; > But got an error. > > As always, tia :) > > James. > > > > -- John Donagher Application Engineer Intacct Corp. - Powerful Accounting on the Web 408-395-0989 720 University Ave. Los Gatos CA 95032 www.intacct.com Public key available off http://www.keyserver.net Key fingerprint = 4024 DF50 56EE 19A3 258A D628 22DE AD56 EEBE 8DDD

« previous php.general (#39268) next »