Multiple returns from a function

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

    Its not possible have two return statement. However it doesn't throw error but when function is called you will receive only first return statement value. We can use return of array to get multiple values in return. For Example:

    function test($testvar)
    {
      // do something
      //just assigning a string for example, we can assign any operation result
      $var1 = "result1";
      $var2 = "result2";
      return array('value1' => $var1, 'value2' => $var2);
    }
    

提交回复
热议问题