-2

My question is can we return multiple return query with single function and call them?

for example:

<?php 
function addition($a, $b, $c, $d)
{
    $x = $a+$b+$c+$d;
    $y = $a+$b;
    $z = $c+$d;
    return $x;
    return $y;
    return $z;
}
?>

Can we call the function and get the value of $x, $y, $z all together.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
JSLearner
  • 3
  • 5
  • 1
    Does this answer your question? [Multiple returns from a function](https://stackoverflow.com/questions/3451906/multiple-returns-from-a-function) – catcon Mar 28 '20 at 06:23
  • Yeah got it. I was just wondering how can i return multiple value. – JSLearner Mar 28 '20 at 06:23

1 Answers1

0

This is how to do, you can only do return once in the function. when ever a return is executed you take the control back from the function and it stops further execution of the function. you could write multiple returns under conditional statements but only one return will be executed.

function addition($a, $b, $c, $d)
{
    $x = $a+$b+$c+$d;
    $y = $a+$b;
    $z = $c+$d;

    $arr = [
        'x' => $x,
        'y' => $y,
        'z' => $z,
    ];
    return $arr;
}

// you can get the values like this

$res = addition($a, $b, $c, $d);

echo $res['x'];
echo $res['y'];
echo $res['z'];
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Umer Abbas
  • 1,866
  • 3
  • 13
  • 19