Multiple returns from a function

后端 未结 30 2320
盖世英雄少女心
盖世英雄少女心 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:40

    I think eliego has explained the answer clearly. But if you want to return both values, put them into a array and return it.

    function test($testvar)
    {
      // do something
    
      return array('var1'=>$var1,'var2'=>$var2);
    //defining a key would be better some times   
    }
    

    //to access return values

    $returned_values = test($testvar);
    
    echo $returned_values['var1'];
    echo $returned_values['var2'];
    

提交回复
热议问题