Multiple returns from a function

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

    In your example, the second return will never happen - the first return is the last thing PHP will run. If you need to return multiple values, return an array:

    function test($testvar) {
    
        return array($var1, $var2);
    }
    
    $result = test($testvar);
    echo $result[0]; // $var1
    echo $result[1]; // $var2
    

提交回复
热议问题