How to return multiple values in PHP

Is it possible to return multiple values through PHP function. In this article, i’ll try to explain how to return multiple values. Return statement can return only single value at a time, but you can use array or object to return multiple values.

How to return multiple values in PHP

If you want to return multiple values in php then make an array and return it.

I show you this through example

function pass_multiple_values($first_arg,$second_arg,$third_arg)
{

 $value1= $first_arg; // Calculated some value and assign it to value1.

 $value2= $secon_arg;

 $value3= $third_arg;
 return array($value1,$value2,$value3);

}

How to retrieve multiple values in PHP

list($val1, $val2, $val3) = pass_multiple_values($first_arg,$second_arg,$third_arg);

Through list function(), you can assign array items to variables.

print_r($val1); 

// It contains the value passed in $value1. Similarly for $val2 and $val3.

Another option is retrieving the multiple values

$get_val = pass_multiple_values($first_arg,$second_arg,$third_arg);

echo $get_val[0];

echo $get_val[1];

echo $get_val[2];
admin

About admin

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , . Bookmark the permalink.