Multiple returns from a function

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

    Some might prefer returning multiple values as object:

    function test() {
        $object = new stdClass();
    
        $object->x = 'value 1';
        $object->y = 'value 2';
    
        return $object;
    }
    

    And call it like this:

    echo test()->x;
    

    Or:

    $test = test();
    echo $test->y;
    

提交回复
热议问题