Multiple returns from a function

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

    Functions in PHP can return only one variable. you could use variables with global scope, you can return array, or you can pass variable by reference to the function and than change value,.. but all of that will decrease readability of your code. I would suggest that you look into the classes.

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

    This is the easiest way to do it:

    public function selectAllUsersByRole($userRole, $selector) {
    
        $this->userRole = $userLevel;
        $this->selector = $selector;
    
        $sql = "SELECT * FROM users WHERE role <= ? AND del_stat = 0";
        $stm = $this->connect()->prepare($sql); // Connect function in Dbh connect to database file
        $stm->execute([$this->userRole]); // This is PHP 7. Use array($this->userRole) for PHP 5
    
        $usersIdArray = array();
        $usersFNameArray = array();
        $usersLNameArray = array();
    
        if($stm->rowCount()) {
            while($row = $stm->fetch()) {
    
                array_push($usersIdArray,    $row['id']);
                array_push($usersFNameArray, $row['f_name']);
                array_push($usersLNameArray, $row['l_name']);
    
                // You can return only $row['id'] or f_name or ...
                // I used the array because it's most used.
            }
        }
        if($this->selector == 1) {
            return $usersIdArray;
        }elseif($this->selector == 2) {
            return $usersFNameArray;
        }elseif($this->selector == 3) {
            return $usersLNameArray;
        }
    
    }
    

    How can we call this function?

    $idData = $selectAllUsers->selectAllUsersByLevel($userRole, 0);
    print_r($idData);
    $idFName = $selectAllUsers->selectAllUsersByLevel($userRole, 1);
    print_r($idFname);
    

    That's it. Very easy.

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-11-22 06:52

    Thought I would expand on a few of the responses from above....

    class nameCheck{
    
    public $name;
    
    public function __construct(){
        $this->name = $name;
    }
    
    function firstName(){
                // If a name has been entered..
        if(!empty($this->name)){
            $name = $this->name;
            $errflag = false;
                        // Return a array with both the name and errflag
            return array($name, $errflag);
                // If its empty..
        }else if(empty($this->name)){
            $errmsg = 'Please enter a name.';
            $errflag = true;
                        // Return both the Error message and Flag
            return array($errmsg, $errflag);
        }
    }
    
    }
    
    
    if($_POST['submit']){
    
    $a = new nameCheck;
    $a->name = $_POST['name'];
    //  Assign a list of variables from the firstName function
    list($name, $err) = $a->firstName();
    
    // Display the values..
    echo 'Name: ' . $name;
    echo 'Errflag: ' . $err;
    }
    
    ?>
    <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" >
    <input name="name"  />
    <input type="submit" name="submit" value="submit" />
    </form>
    

    This will give you a input field and a submit button once submitted, if the name input field is empty it will return the error flag and a message. If the name field has a value it will return the value/name and a error flag of 0 for false = no errors. Hope this helps!

    0 讨论(0)
  • 2020-11-22 06:54
    $var1 = 0;
    $var2 = 0;
    
    function test($testvar, &$var1 , &$var2)
    {
      $var1 = 1;
      $var2 = 2;
      return;
    }
    test("", $var1, $var2);
    
    // var1 = 1, var2 = 2 
    
    

    It's not a good way, but I think we can set two variables in a function at the same time.

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

    Technically, you can't return more than one value. However, there are multiple ways to work around that limitation. The way that acts most like returning multiple values, is with the list keyword:

    function getXYZ()
    {
        return array(4,5,6);
    }
    
    list($x,$y,$z) = getXYZ();
    
    // Afterwards: $x == 4 && $y == 5 && $z == 6
    // (This will hold for all samples unless otherwise noted)
    

    Technically, you're returning an array and using list to store the elements of that array in different values instead of storing the actual array. Using this technique will make it feel most like returning multiple values.

    The list solution is a rather php-specific one. There are a few languages with similar structures, but more languages that don't. There's another way that's commonly used to "return" multiple values and it's available in just about every language (in one way or another). However, this method will look quite different so may need some getting used to.

    // note that I named the arguments $a, $b and $c to show that
    // they don't need to be named $x, $y and $z
    function getXYZ(&$a, &$b, &$c)
    {
        $a = 4;
        $b = 5;
        $c = 6; 
    }
    
    getXYZ($x, $y, $z);
    

    This technique is also used in some functions defined by php itself (e.g. $count in str_replace, $matches in preg_match). This might feel quite different from returning multiple values, but it is worth at least knowing about.

    A third method is to use an object to hold the different values you need. This is more typing, so it's not used quite as often as the two methods above. It may make sense to use this, though, when using the same set of variables in a number of places (or of course, working in a language that doesn't support the above methods or allows you to do this without extra typing).

    class MyXYZ
    {
        public $x;
        public $y;
        public $z;
    }
    
    function getXYZ()
    {
        $out = new MyXYZ();
    
        $out->x = 4;
        $out->y = 5;
        $out->z = 6;
    
        return $out;
    }
    
    $xyz = getXYZ();
    
    $x = $xyz->x;
    $y = $xyz->y;
    $z = $xyz->z;
    

    The above methods sum up the main ways of returning multiple values from a function. However, there are variations on these methods. The most interesting variations to look at, are those in which you are actually returning an array, simply because there's so much you can do with arrays in PHP.

    First, we can simply return an array and not treat it as anything but an array:

    function getXYZ()
    {
        return array(1,2,3);
    }
    
    $array = getXYZ();
    
    $x = $array[1];
    $y = $array[2];
    $z = $array[3];
    

    The most interesting part about the code above is that the code inside the function is the same as in the very first example I provided; only the code calling the function changed. This means that it's up to the one calling the function how to treat the result the function returns.

    Alternatively, one could use an associative array:

    function getXYZ()
    {
        return array('x' => 4,
                     'y' => 5,
                     'z' => 6);
    }
    
    $array = getXYZ();
    
    $x = $array['x'];
    $y = $array['y'];
    $z = $array['z'];
    

    Php does have the compact function that allows you to do same as above but while writing less code. (Well, the sample won't have less code, but a real world application probably would.) However, I think the amount of typing saving is minimal and it makes the code harder to read, so I wouldn't do it myself. Nevertheless, here's a sample:

    function getXYZ()
    {
        $x = 4;
        $y = 5;
        $z = 6;
    
        return compact('x', 'y', 'z');
    }
    
    $array = getXYZ();
    
    $x = $array['x'];
    $y = $array['y'];
    $z = $array['z'];
    

    It should be noted that while compact does have a counterpart in extract that could be used in the calling code here, but since it's a bad idea to use it (especially for something as simple as this) I won't even give a sample for it. The problem is that it will do "magic" and create variables for you, while you can't see which variables are created without going to other parts of the code.

    Finally, I would like to mention that list doesn't really play well with associative array. The following will do what you expect:

    function getXYZ()
    {
        return array('x' => 4,
                     'y' => 5,
                     'z' => 6);
    }
    
    $array = getXYZ();
    
    list($x, $y, $z) = getXYZ();
    

    However, the following will do something different:

    function getXYZ()
    {
        return array('x' => 4,
                     'z' => 6,
                     'y' => 5);
    }
    
    $array = getXYZ();
    
    list($x, $y, $z) = getXYZ();
    
    // Pay attention: $y == 6 && $z == 5
    

    If you used list with an associative array, and someone else has to change the code in the called function in the future (which may happen just about any situation) it may suddenly break, so I would recommend against combining list with associative arrays.

    0 讨论(0)
提交回复
热议问题