Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning multiple values from Function execution 3

Status
Not open for further replies.

aspx

Programmer
Jul 25, 2002
52
BG
Hi!
Function MyF ($param)
{
.....//some calculations
return $value1;//Integer
return $value2;//Boolean
return $value3;//String
};
But how to access the values returned by the function MyF??

Thanx for any ideas!
 
Hi aspx,
You can't return multiple values from a function, however you can use lists.

function small_numbers()
{
return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();

I'm sure this will work with mixed data types as well.
[cheers]
Cheers!
Laura
 
Thanx a lot, Laura[love2]!
It works fine for different types and even for multidimenssional Array!!
Yes, PHP is much better than ASP...

It's time for beer - CHEERS!

Aspx.
 
Even better, if you use an array as your function argument, you can now emulate named parameters, arbitrary additional arguments, etc...

Code:
<?php

  function my_function($array_args)
  {
    extract($array_args);

    // do whatever you want with $var1, $var2, etc...
  }

  // Then you can just call it with:

  $returnval = myfunction(array(
    $var1=>&quot;value1&quot;,
    $var2=>&quot;value2&quot;,
    $var3=>&quot;etc...&quot;
    );
?>

But does this method support defaults? Why not?
Code:
<?php

// default args in array
function do_something($arr = array('var1'=>&quot;default1&quot;,'var2'=>&quot;default2&quot;))
{
     echo $arr[&quot;var1&quot;] . '--' . $arr[&quot;var2&quot;];
}

// call with no args, outputs defaults
do_something();

echo &quot;<br><br><br>&quot;;

// call with args, outputs args
do_something(array(
  &quot;var1&quot;=>&quot;Something else&quot;,
  &quot;var2&quot;=>&quot;Some more of that&quot;));

?>

Now, to add arbitrary arguments to the above, you could just add any number of array members besides &quot;var1&quot; and &quot;var2&quot;, and do some sort of recursive operation with them. PHP supports arrays of mixed type, so you could even do something like:
Code:
<?php

function do_something($arr = array(
  'var1'=>&quot;default1&quot;,
  'var2'=>&quot;default2&quot;))
  {
     echo $arr[&quot;var1&quot;] . '--' . $arr[&quot;var2&quot;] . &quot;<br>\n&quot;;

    // arbitrary numbered args:
    $i = 0;
    while($arr[$i]){
        echo $arr[$i] . &quot;<br>\n&quot;;
        $i++;   
    }   
    
  }

// call with args, outputs args
do_something(array(
  &quot;var1&quot;=>&quot;Something insane&quot;,
  &quot;var2&quot;=>&quot;More more more&quot;,
  0=>&quot;first arbitrary arg&quot;,
  1=>&quot;second arbitrary arg&quot;,
  2=>&quot;third arbitrary arg&quot;
));

?>

Fun stuff ;-). -------------------------------------------

Big Brother: &quot;War is Peace&quot; -- Big Business: &quot;Trust is Suspicion&quot;
(
 
Thank you very much, Ricamor![sunshine]
 
rycamor, thanks that fixed a problem which just landed on my desk!!!

STAR

//Karv ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
or, you can use references.

function ($normalparam, &$returnparm1, &$returnparm2, &$returnparm3) {
$returnparm1 = $normalparam*5;
$returnparm2 = 'string';
$returnparm3 = $normalparam . 'x';
} --BB
 
For those who didn't catch what is going on in BB101's method, the function is modifying the variables directly, rather than returning them. The variables that are passed as $returnparm1, 2 and 3 are passed by reference, so when they are modified inside the function, it is actually the referenced variable outside that is modified (
Sometimes there are benefits to doing things this way, and it can also save memory in situations where performance is critical, but it can also make code messy to maintain later, because your variable is being modified in a non-obvious way.

One can also accomplish such things by using the
Code:
global
keyword, as in
Code:
function change_globally($normalparam) {

  global  $originalparm1, $originalparm2, $originalparm3;

  $originalparm1 = $normalparam*5;
  $originalparm2 = 'string';
  $originalparm3 = $normalparam . 'x';
}

Now, your function is not even passing by reference, but using the &quot;outside&quot; variables directly, thus modifying them in the global scope of your script. This can also be useful sometimes, but again, be careful, or it can make your code hard to maintain or extend. -------------------------------------------

Big Brother: &quot;War is Peace&quot; -- Big Business: &quot;Trust is Suspicion&quot;
(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top