php

Return Multiple Values From the Function in PHP

The function can return value to the caller by using the return statement in any programming language. This statement can also be used in PHP to return single and multiple values from the function. Array and object variables are used to store multiple values. Multiple values can be returned from the PHP function by using these types of variables. This tutorial shows the ways to return multiple values from the function using PHP script.

Example 1: Return multiple values as an array

Create a PHP file with the following script to return multiple values as an array. Here, the function named Function1() is defined to assign five values into five variables. Next, these values will be returned as an array to the caller. The returned values will be stored in an array variable named $languages printed by using the var_dump() function.

<?php

//Define PHP function
function Function1(){

    //Assign values to five variables
    $lang1 = "HTML";
    $lang2 = "PHP";
    $lang3 = "JAVA";
    $lang4 = "JavaScript";
    $lang5 = "XML";

    //Return multiple values to the caller using array
    return array($lang1, $lang2, $lang3, $lang4, $lang5);
}

//Store the return values
$languages = Function1();

//Print the returned values with datatypes
var_dump($languages);

?>

Output:

The following output will appear after running the above script from the webserver.

Example 2: Store the returned values into multiple variables

Create a PHP file with the following script to return multiple values using an array and store them into multiple variables. Here, the function named employee_details() is defined that stored employee information in six variables and these variables are returned by using an array to the caller. An array with six variables is declared to receive the returned values from the function. Next, these variables are printed after calculating the total salary of the employee.

<?PHP

//Define PHP function
function employee_details(){

    //Assign employee's details
    $name = "Mehr Nigar";
    $email = "[email protected]";
    $phone = "8801825763564";
    $basic = 40000;
    $house_rent = 10000;
    $transport = 5000;

    //Return values using array
    return [$name, $email, $phone, $basic, $house_rent, $transport];
}

//Store array values into variables
[$n, $e, $p, $b, $h, $r] = employee_details();

//Calculate salary amount
$salary = $b + $h + $r;

//Print the employee details
echo "<b>Employee Details:</b><br/>";
echo "Name: $n<br/>Email: $e<br/>Phone: $p<br/>Salary: $salary";

?>

Output:

The following output will appear after running the above script from the webserver. Six values are returned from the function and stored in six variables. The salary of the employee is calculated by adding the values of the last three variables. Name, Email, Phone, and total salary has been printed here as output.

Example 3: Return multiple values based on the conditional statement

Create a PHP file with the following script to return multiple values based on the conditional statement. Here, the function named arithmetic_operation() is defined that can take three arguments. The first argument will take any operator symbol (‘+’, ‘-‘, ‘x’, ‘/’), the second and the third argument will take any numeric value. The function will calculate addition or subtraction or multiplication or division based on the value of $operator and return the values of the $value1, $value2, and $result as an array.

<?php

//Define PHP function with arguments
function arithmetic_operation($operator, $value1, $value2)
{

    //Perform arithmetic operations based on operator
    if($operator == '+'){
        $result = $value1 + $value2;
    }
    elseif($operator == '-'){
        $result = $value1 - $value2;
    }
    elseif($operator == 'x'){
        $result = $value1 * $value2;
    }
    elseif($operator == '/'){
        $result = $value1 / $value2;
    }
    else{
        $result = "Operator is undefined";
    }

    //Return multiple values to the caller
    return array($value1, $value2, $result);

}

//Assign operator
$operator = '-';
//Store the return values from the function
$values = arithmetic_operation($operator, 80, 50);
//Print the return values
echo $values[0]." ".$operator." ".$values[1]." = ".$values[2];
?>

Output:

The following output will appear after running the above script from the webserver. ‘-‘ symbol is given as operator and, 80 and 50 are given for the second and third arguments of the function. So, the subtraction value of 80-50 is 30 that is printed as output.

Example 4: Return multiple values using the yield keyword

Create a PHP file with the following script that will return multiple values by using the yield keyword. Here, the user_data() function is used to return three multiple values to the caller. The return values will be printed using the ‘for’ loop.

<?php

//Define PHP function
function user_data(){
    //Return multiple values using yield
    yield "[email protected]";
    yield "abir990";
    yield "845245";
}

//Store the return values
$user = user_data();

echo "<b>User's details:</b><br/>";
//Print the return values using loop
foreach($user as $value){
    echo $value."<br/>";
}

?>

Output:

The following output will appear after running the above script from the webserver. Here, the return values are printed in each line by using the ‘for’ loop.

Example 5: Return multiple values of an array after filtering  the data

Create a PHP file with the following script to return all even numbers from an array. Here, the filter_even() function is used to take an array of numeric values as an argument, find out and store the even numbers from the array into another array variable and return the new array to the caller.

<?php

//Define PHP function
function filter_even($num_array) {

    //Declare an empty array
    $even_numbers = array();

    //Iterate the values of the array using loop
    foreach( $num_array as $num ) {
        if( $num % 2 == 0 )
            array_push( $even_numbers, $num );
    }

    //Return the filtered array to the caller
    return $even_numbers;

}

//Declare a numeric array
$numbers = array(8, 55, 23, 10, 4, 91, 39, 48);

echo "The list of numbers before filtering: <br/>";
//Print array before filtering
foreach( $numbers as $val ) {
    echo $val . "¬† ";
}

//Store the even numbers
$values = filter_even($numbers);

echo "<br/><br/>The list of even numbers: <br/>";
//Print the return values
foreach( $values as $val ) {
    echo $val . "¬† ";

}

?>

Output:

The following output will appear after running the above script from the webserver. The output shows the values of the main array and filtered array.

Conclusion

The way of returning multiple values from a function using an array variable has shown in this tutorial by using different examples. You can also use the object variable to return the multiple values from the function using PHP script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.