Multiple returns from a function

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

    In PHP 5.5 there is also a new concept: generators, where you can yield multiple values from a function:

    function hasMultipleValues() {
        yield "value1";
        yield "value2";
    }
    
    $values = hasMultipleValues();
    foreach ($values as $val) {
        // $val will first be "value1" then "value2"
    }
    

提交回复
热议问题