Multiple returns from a function

后端 未结 30 2312
盖世英雄少女心
盖世英雄少女心 2020-11-22 06:12

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

function test($testvar)
{
  // Do something

  return $var1;
  return $var2;
}
<
30条回答
  •  情话喂你
    2020-11-22 06:57

    I know that I am pretty late, but there is a nice and simple solution for this problem.
    It's possible to return multiple values at once using destructuring.

    function test()
    {
        return [ 'model' => 'someValue' , 'data' => 'someothervalue'];
    }
    

    Now you can use this

    $result = test();
    extract($result);
    

    extract creates a variable for each member in the array, named after that member. You can therefore now access $model and $data

提交回复
热议问题