Question

Just curious to know what the best practice would be for something like this:

A function, that returns multiple variables - how should one return these variables?

like this (globalizing):

function myfun(){

global $var1,$var2,$var3;

$var1="foo";
$var2="foo";
$var3="foo";

}//end of function

or like this (returning an array):

function myfun(){

$var1="foo";
$var2="foo";
$var3="foo";

$ret_var=array("var1"=>$var1,"var2"=>$var2,"var3"=>$var3);

return $ret_var;

}//end of function

I done a performance test, and it looks like using arrays is faster (after a few refreshes):

array took: 5.9999999999505E-6
global took: 2.0999999999938E-5

But I'm curious to know which method is the best practiced for a simple situation like this?

Was it helpful?

Solution

You should absolutely return an array. It is much clearer. You can even use the list construct to simulate multiple return values a-la python:

function foon() {
   return array('one', 'two', 'three');
}
list($one, $two, $three) = foon();

Edit: in some cases an associative array is also quite appropriate. I can't imagine the performance difference would merit this loss of clarity.

OTHER TIPS

Use references (if it makes sense) http://de.php.net/manual/en/language.references.php

Example:

function myfun(&$var1, &$var2, &$var3)
{
    $var1="foo";
    $var2="foo";
    $var3="foo";
}

Or return an array.

AND NEVER USE GLOBALS! It is very bad design and not maintainable.

The associative array option is better from a code maintenance standpoint since you're not mucking-up the global namespace. Will also be easier for others to read your code.

You could return an Array and use list to get the values, see http://www.php.net/manual/en/function.list.php

function multiple() {
    return array('val1', 'anotherval');
}

list($var1, $var2) = multiple();

echo "$var1, $var2";

Works for me, I don't know how fast that is, but maybe it looks cleaner in your code.

You can use class http://php.net/manual/en/keyword.class.php.

class Vari {
    var $var1;
    var $var2;
    var $var3;
}

function myfun() {    
    $var = new Vari;
    $var->var1="foo";
    $var->var2="foo";
    $var->var3="foo";
    return $var;    
}

$v = myfun();
echo $v->var1;
/*foo*/

Since this ranks so high on Google and the fact that it took me a while to find a clear answer myself to something so simple...

To give a complete answer and thoroughly clarify how to return multiple variables that you can actually use independently outside of your function:

  function  myFunction() {

      //your code that returns your results to variables below

     $var1 = 'message 1';
     $var2 = 'message 2';
     $var3 = 'message 3';

     // must put all variables inside an array to get them outside of function
     $return_array = ($var1, $var2, $var3);

     // return your array inside function
     return $return_array;
  }



 // call function (returns all variables in array)
 $get_results = myFunction();

 // call and assign individual variables outside of function
 $var1 = $get_results[0];
 $var2 = $get_results[1];
 $var3 = $get_results[2];



 // now you can use individual variables outside of function for example:

 echo $var2;   //will now output: message 2
 echo $var3;   //will now output: message 3

If you are using results from database query inside your function more than likely you will need to pass your connection variable into the function. For example, we will say $db is our database connection and you would just modify the two lines of code below:

  //original code to change
  function  myFunction() {
  //change to
  function  myFunction($db) {

  //original code to change
  $get_results = myFunction();
  //change to
  $get_results = myFunction($db);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top