Multiple returns from a function

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

    You can always only return one variable which might be an array. But You can change global variables from inside the function. That is most of the time not very good style, but it works. In classes you usually change class varbiables from within functions without returning them.

    0 讨论(0)
  • 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:

    <?PHP
    function myfunc($a,$b){
       return array('foo'=>$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.

    0 讨论(0)
  • 2020-11-22 06:32

    Yes and no. You can't return more than one variable / object, but as you suggest, you can put them into an array and return that.

    There is no limit to the nesting of arrays, so you can just package them up that way to return.

    0 讨论(0)
  • 2020-11-22 06:32

    Languages which allow multiple returns usually just convert the multiple values into a data structure.

    For example, in Python you can return multiple values. However, they're actually just being returned as one tuple.

    So you can return multiple values in PHP by just creating a simple array and returning that.

    0 讨论(0)
  • 2020-11-22 06:33

    Add all variables in an array and then finally return the array.

    function test($testvar)
    {
      // do something
      return array("var1" => $var1, "var2" => @var2);
    }
    

    And then

    $myTest = test($myTestVar);
    //$myTest["var1"] and $myTest["var2"] will be usable
    
    0 讨论(0)
  • 2020-11-22 06:34

    There is no way of returning 2 variables. Although, you can propagate an array and return it; create a conditional to return a dynamic variable, etc.

    For instance, this function would return $var2

    function wtf($blahblah = true) {
        $var1 = "ONe";
        $var2 = "tWo";
    
        if($blahblah === true) {
          return $var2;
        }
        return $var1;
    }
    

    In application:

    echo wtf();
    //would echo: tWo
    echo wtf("not true, this is false");
    //would echo: ONe
    

    If you wanted them both, you could modify the function a bit

    function wtf($blahblah = true) {
        $var1 = "ONe";
        $var2 = "tWo";
    
        if($blahblah === true) {
          return $var2;
        }
    
        if($blahblah == "both") {
          return array($var1, $var2);
        }
    
        return $var1;
    }
    
    echo wtf("both")[0]
    //would echo: ONe
    echo wtf("both")[1]
    //would echo: tWo
    
    list($first, $second) = wtf("both")
    // value of $first would be $var1, value of $second would be $var2
    
    0 讨论(0)
提交回复
热议问题