Michiel
Michiel

Reputation: 8103

2 returns in one function

I would like to return 2 different values with one function. These are my functions:

function example1 ($a) {
    return $result1;
    return $result2;
}

function example2 ($edit, $account, $value) {
    $config ($edit);
}

Like this I get $result1 in my $config-variable. But what do I need to do to get both returned values in my example1-function?

Upvotes: 3

Views: 1885

Answers (9)

Armin
Armin

Reputation: 15978

It is not possible to make more than one return in one function or method. But you have the possibility to return an array with several values instead:

function example1 ($a) {
    return array('top' => $result1, 'left' => $result2);
}

Now you can access the two values:

$result = example1($a);
echo $result['top'] . ' x ' . $result['left'];

Upvotes: 0

Savageman
Savageman

Reputation: 9487

Several solutions:

  1. Use arrays : return array($result1, $result2); You can then use list($config1, $config2) = $returned; to get both values assigned to 2 variables.

  2. Use references.

    function example_1($a, &$result2) {
      $result2 = 'whatever';
      return $result1;
    }
    
    // $config1 will contain $result1
    // $config2 will contain $result2 ('whatever' in this case)
    $config1 = example_1($a, $config_2);
    

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191819

Php does not allow for multiple returns from the same function (as python does for example) so if you want to do this you have to either return an array, an object, or redo your logic.

Or redo your logic to return an array of objects.

Upvotes: 1

Logan Serman
Logan Serman

Reputation: 29880

This is probably the cleanest way to do this:

function example1($a)
{
    return array($result1, $result2)
}

// get both values
list($config1, $config2) = example1($a);

This will make $config = $result1 and $config2 = $result2.

Upvotes: 4

Krzysztof
Krzysztof

Reputation: 16150

Return array:

function example1 ($a) {
    return array($result1, $result2);
}

And use list to retrieve it:

list($a, $b) = example1('something');

Upvotes: 0

Yogu
Yogu

Reputation: 9455

You could use pass-by-reference parameters:

function example(&$result1, &$result2) {
  $result1 = 'a';
  $result2 = 123;
}

example($a, $b);
echo "$a, $b";

Upvotes: 0

matino
matino

Reputation: 17735

You can't return 2 variables in one function, you should return an array instead:

function example1 ($a) {
    $result[0] = 'something';
    $result[1] = 'something else';
    return $result;
}

Upvotes: 3

Darvex
Darvex

Reputation: 3654

You just need to put your results into array and then return said array

function example1 ($a) {
    $array = array();
    $array[0] = $result1;
    $array[1] = $result2;
    return $array;
    }

Upvotes: 1

DhruvPathak
DhruvPathak

Reputation: 43265

use associative array or stdClass as return variable .

function example1 ($a) {
    return array("result_1" => $result1 , "result_2" => $result2 );
}

And retreive the values wherever needed.

$answer = example1($a);
$firstResult = $answer["result_1"];
$secondResult = $answer["result_2"];

Note however that, this will not work :

 $firstResult = example1($a)["result_1"] ;

and will give a syntax error, due to a shortcoming in PHP. Hence,assign the array first, then retreive the values.

Upvotes: 1

Related Questions