How to Return Multiple Values from a PHP Function Using Arrays, References, Objects, and Destructuring

This guide explains several practical techniques for returning multiple values from a PHP function, including indexed and associative arrays, reference parameters, object or anonymous class returns, and PHP 7.1+ destructuring assignment, with clear code examples for each method.

php Courses
php Courses
php Courses
How to Return Multiple Values from a PHP Function Using Arrays, References, Objects, and Destructuring

PHP functions cannot directly return multiple values, but developers can achieve the effect by packaging data into arrays, objects, or using reference parameters.

Returning Multiple Values with Arrays

The simplest approach is to place the values in an indexed array and unpack them with list() or array indexing.

function getUserInfo() {
    $name = "张三";
    $age = 25;
    $city = "北京";
    return [$name, $age, $city]; // indexed array
}
list($userName, $userAge, $userCity) = getUserInfo();
echo $userName; // 输出:张三

Using an associative array improves readability by naming each element.

function getUserInfo() {
    return [
        'name' => $name,
        'age'  => $age,
        'city' => $city,
    ];
}
$result = getUserInfo();
echo $result['name']; // 输出:张三

Using Reference Parameters

By prefixing parameters with &, a function can modify variables passed from the caller, effectively returning several values.

function calculate($a, $b, &$sum, &$product) {
    $sum = $a + $b;
    $product = $a * $b;
}
calculate(3, 4, $add, $mul);
echo $add; // 输出:7
echo $mul; // 输出:12

This method is useful when many variables need to be updated, but it can reduce code clarity.

Returning an Object or Anonymous Class

When the returned data structure is more complex, encapsulating it in an object (or anonymous object) provides better organization and extensibility.

function getRectangleInfo($width, $height) {
    return (object) [
        'area'      => $width * $height,
        'perimeter'=> 2 * ($width + $height),
    ];
}
$rect = getRectangleInfo(5, 3);
echo $rect->area;      // 输出:15
echo $rect->perimeter; // 输出:16

Destructuring Assignment (PHP 7.1+)

PHP 7.1 introduced a clearer destructuring syntax for associative arrays, enhancing readability.

function getCoordinates() {
    return ['x' => 10, 'y' => 20, 'z' => 30];
}
['x' => $x, 'y' => $y, 'z' => $z] = getCoordinates();
echo $x; // 输出:10

In practice, arrays are the most flexible solution, objects suit complex structures, and reference parameters should be used cautiously. Choose the method that best matches the specific requirements of your code.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPArrayfunctionreferenceDestructuringobjectreturn-multiple-values
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.