Can you return multiple values in php?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    PHP doesn’t support to return multiple values in a function. Inside a function when the first return statement is executed, it will direct control back to the calling function and second return statement will never get executed. However, there are ways to work around this limitation. 
    The multiple values can be returned from a function by using an array.
    Example 1: This example shows how to return multiple values from a function in PHP. First, create an empty array end push the element to the array and then return the array.
     

    php

    function factors( $n ) {

        $fact = array();

        for ( $i = 1; $i < $n; $i++) {

            if( $n % $i == 0 )

                array_push( $fact, $i );

        }

        return $fact;

    }

    $num = 24;

    $nFactors = factors($num);

    echo 'Factors of ' . $num . ' are:
    '
    ;

    foreach( $nFactors as $x ) {

        echo $x . "
    ";

    }

    ?>

    Output:

    Factors of 24 are: 
    1
    2
    3
    4
    6
    8
    12

    Example 2: This example uses list function to store the swapped value of variable. It is used to assign array values to multiple variables at the same time. The multiple values returned in array from the function can be assigned to corresponding variables using list().
     

    php

    function swap( $x, $y ) {

        return array( $y, $x );

    }

    $a = 10;

    $b = 20;

    echo 'Before swapping the elements
    '
    ;

    echo 'a = ' . $a . '
    '
    . 'b = ' . $b . '
    '
    ;

    list($a, $b) = swap($a, $b);

    echo 'After swapping the elements
    '
    ;

    echo 'a = ' . $a . '
    '
    . 'b = ' . $b . '
    '
    ;

    ?>

    Output:

    Before swapping the elements 
    a = 10
    b = 20
    After swapping the elements 
    a = 20
    b = 10

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    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[0];
    $y = $array[1];
    $z = $array[2];
    

    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.

    How can I return multiple data from a function in PHP?

    PHP doesn't support to return multiple values in a function. Inside a function when the first return statement is executed, it will direct control back to the calling function and second return statement will never get executed.

    Can return return multiple values?

    JavaScript functions can return a single value. To return multiple values from a function, you can pack the return values as elements of an array or as properties of an object.

    How do I return multiple values from a return statement?

    We can return more than one values from a function by using the method called “call by address”, or “call by reference”. In the invoker function, we will use two variables to store the results, and the function will take pointer type data.

    How pass multiple values in array in PHP?

    Create an instance for your view file and use predefined method of a controller file to set variables. Like this way: return new ViewModel(array( 'order_by' => $order_by, 'order' => $order, 'page' => $page, 'paginator' => $paginator, ));