Multiple returns from function

2018-12-31 18:26发布

Is it possible to have a function with 2 returns like this:

function test($testvar)
{
  // do something

  return $var1;
  return $var2;
}

If so, how would I be able to get each return separately?

标签: php
28条回答
宁负流年不负卿
2楼-- · 2018-12-31 19:01

There is no way of returning 2 variables. Although, you can propagate an array and return it; create a conditional to return a dynamic variable, etc.

For instance, this function would return $var2

function wtf($blahblah = true) {
    $var1 = "ONe";
    $var2 = "tWo";

    if($blahblah === true) {
      return $var2;
    }
    return $var1;
}

In application:

echo wtf();
//would echo: tWo
echo wtf("not true, this is false");
//would echo: ONe

If you wanted them both, you could modify the function a bit

function wtf($blahblah = true) {
    $var1 = "ONe";
    $var2 = "tWo";

    if($blahblah === true) {
      return $var2;
    }

    if($blahblah == "both") {
      return array($var1, $var2);
    }

    return $var1;
}

echo wtf("both")[0]
//would echo: ONe
echo wtf("both")[1]
//would echo: tWo

list($first, $second) = wtf("both")
// value of $first would be $var1, value of $second would be $var2
查看更多
骚的不知所云
3楼-- · 2018-12-31 19:02
<?php
function foo(){
  $you = 5;
  $me = 10;
  return $you;
  return $me;
}

echo foo();
//output is just 5 alone so we cant get second one it only retuns first one so better go with array


function goo(){
  $you = 5;
  $me = 10;
  return $you_and_me =  array($you,$me);
}

var_dump(goo()); // var_dump result is array(2) { [0]=> int(5) [1]=> int(10) } i think thats fine enough

?>
查看更多
弹指情弦暗扣
4楼-- · 2018-12-31 19:03

The answer is no. When the parser reaches the first return statement, it will direct control back to the calling function - your second return statement will never be executed.

查看更多
泛滥B
5楼-- · 2018-12-31 19:05

Or you can pass by reference:

function byRef($x, &$a, &$b)
{
    $a = 10 * $x;
    $b = 100 * $x;
}

$a = 0;
$b = 0;

byRef(10, $a, $b);

echo $a . "\n";
echo $b;

This would output

100
1000
查看更多
泛滥B
6楼-- · 2018-12-31 19:05

Its not possible have two return statement. However it doesn't throw error but when function is called you will receive only first return statement value. We can use return of array to get multiple values in return. For Example:

function test($testvar)
{
  // do something
  //just assigning a string for example, we can assign any operation result
  $var1 = "result1";
  $var2 = "result2";
  return array('value1' => $var1, 'value2' => $var2);
}
查看更多
何处买醉
7楼-- · 2018-12-31 19:05

This is the easiest way to it

public function selectAllUsersByRole($userRole, $selector){
    $this->userRole = $userLevel;
    $this->selector = $selector;

    $sql = "SELECT * FROM users WHERE role <= ? AND del_stat = 0";
    $stm = $this->connect()->prepare($sql);// connect function in Dbh connect to database file
    $stm->execute([$this->userRole]);//this is php7, use array($this->userRole) for php5

    $usersIdArray = array();
    $usersFNameArray = array();
    $usersLNameArray = array();

    if($stm->rowCount()){
        while($row = $stm->fetch()){

            array_push($usersIdArray, $row['id']);
            array_push($usersFNameArray, $row['f_name']);
            array_push($usersLNameArray, $row['l_name']);
// you cane return only $row['id'] or f_name or .....
//I used the array cause its most using
        }
    }
    if($this->selector == 1) {
        return $usersIdArray;
    }elseif($this->selector == 2) {
        return $usersFNameArray;
    }elseif($this->selector == 3) {
        return $usersLNameArray;
    }

}

How to call this function?

$idData= $selectAllUsers->selectAllUsersByLevel($userRole,0);
print_r($idData);
$idFName= $selectAllUsers->selectAllUsersByLevel($userRole,1);
print_r($idFname);

That's it, very rasy

查看更多
登录 后发表回答