Return Multiple Values From Function Using Arrays In PHP


If you are a C/C++ programmer, then you know that functions can not return more than one value. To over come this, they use Call by Reference – where address of the variables is passed, hence whenever the value in the passed address changes, the value of the actual variable changes without anything being returned.

In PHP we can accomplish this with the help of arrays.

Video Tutorial: Return Multiple Values From Function Using Arrays In PHP



YouTube Link: https://www.youtube.com/watch?v=HsX2ay702R8 [Watch the Video In Full Screen.]


Work Flow: Logic

Step 1: Here we pass the values to the function.

Step 2: Calculation or operation takes place inside the function.

Step 3: In case there are multiple things to return, we store it inside a array variable. Ex: $result = array($add, $sub, $mul, $div);

Step 4: We return the array variable.

Step 5: Now using array index we display the individual result using echo or print statement.

Source Code: Return Multiple Values From Function Using Arrays In PHP

<html>
 <head><title>Return Multiple Values From Functions, 
                                           using Arrays: In PHP</title></head>
 <body>
 
<?php
$get_result = calc(20, 10);
 
echo "Addition: ".$get_result[0]."<br />Subtration :".$get_result[1].
"<br />Multiplication: ".$get_result[2]."
                         <br />Division: ".$get_result[3];
echo "<br /><br />";
 
print_r ($get_result);
 
?>
 
<?php
function calc($one, $two)
{
$add = $one + $two;
$sub  =  $one  -  $two;
$mul = $one * $two;
$div  =  $one  / $two;
 
$result = array($add, $sub, $mul, $div);
 
return $result;
}
?>
 
 </body>
</html>

Output:
Addition: 30
Subtration :10
Multiplication: 200
Division: 2

Array ( [0] => 30 [1] => 10 [2] => 200 [3] => 2 )

Things To Remember:

1. All PHP files must have .php file extension.

2. Concatination operator in PHP is . [DOT]

3. print_r is a function which takes array variable as its argument and displays the input array structure. Ex: print_r ($get_result);

4. Function is defined in php using the keyword function, followed by function name.

5. $ is used to declare variables in PHP.

Array Syntax In PHP:
$variable = array(arrayElements);

For Numbers: Ex: $num = array(20, 40, 50, 60, 100);
For Strings: Ex: $str = array(“Microsoft”,”Apple”,”Oracle”,”Google”);
For Character: Ex: $chr = array(‘A’,’E’,’I’,’O’,’U’);

array is a keyword for declaring/initializing arrays in PHP.

One thought on “Return Multiple Values From Function Using Arrays In PHP”

  1. Is there a way to return two arrays with a function? like sorta like this but i can’t really test this code right now…

    $n = “Jim”;
    $p = “Hi”;
    $i = 1;
    $array1 = test($n,$p,$i)[0];
    $array2 = test($n,$p,$i)[1];
    echo $array1[0]; //would output Jim
    echo $array1[1]; //Would output Hi
    echo $array1[2]; //would output 1
    echo $array2[0]; //would output 2
    echo $array2[1]; //would output 0
    function test($name,$pass,$id)
    {
    $user = array($name,$pass,$id);
    $blah = array($id + 1,0);
    $return = array($user,$blah);

    return $return;
    }

Leave a Reply to Nathan Cancel reply

Your email address will not be published. Required fields are marked *