Multiple returns from a function

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

    Functions, by definition, only return one value.

    However, as you assumed, that value can be an array.

    So you can certainly do something like:

    $a,'bar'=>$b);
    }
    print_r(myfunc('baz','bork'));
    

    That said, it's worth taking a moment and thinking about whatever you're trying to solve. While returning a complex result value (like an array, or an object) is perfectly valid, if you're thinking is that "I want to return two values", you might be designing poorly. Without more detail in your question, it's hard to say, but it never hurts to stop and think twice.

提交回复
热议问题