Multiple returns from a function

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

    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
    

提交回复
热议问题