How to pass an array into a function, and return the results with an array

How to pass an array into a function, and return the results with an array

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Passing Arrays by Reference Solves PHP's Return Limitation

The snippet you are looking at demonstrates a classic stumbling block for developers transitioning into PHP: the language's default argument passing mechanism. In your example, $waffles[3] remains undefined or holds its original value because the function foo modifies a copy of the array, not the original variable itself. This behavior stems from how PHP handles function arguments by default. When you pass $waffles to foo, PHP creates a new instance of the array inside that function scope. Any changes made to $array within foo—like calculating the sum and assigning it to index 3—are isolated to that specific memory block. Once the function returns, the original $waffles variable in your global scope is completely untouched, which is why echo $waffles[3] fails to output the expected result.

To achieve the goal of modifying an array passed into a function and seeing those changes reflected immediately outside the function, you must explicitly pass the array by reference. This is done by prepending an ampersand (&) to the parameter definition. When you declare function foo(&$array), PHP no longer copies the array data; instead, it binds the internal $array variable directly to the memory address of the original $waffles variable. Now, when you execute $array[3] = $array[0] + $array[1] + $array[2]; inside the function, you are effectively writing to $waffles[3] in the global scope.

Here is how the corrected code structure looks:

function foo(&$array) {
    $array[3] = $array[0] + $array[1] + $array[2];
    return $array;
}

$waffles[0] = 1;
$waffles[1] = 2;
$waffles[2] = 3;

foo($waffles); // Pass by reference here

echo $waffles[3]; // Outputs: 6

It is important to note that the return statement in this specific scenario is technically redundant. Since the function modifies the original data through reference and you are accessing $waffles directly afterward, returning the modified array isn't strictly necessary unless you need to assign the result back to a new variable or chain method calls. However, returning the array does not break functionality; it simply provides the caller with explicit access to the processed data if they choose to use it.

This technique of using references is the idiomatic workaround for "returning multiple values" in PHP prior to version 7.4, where native support for multiple return values via [...,] was introduced. While modern versions of PHP allow functions like foo() to return an array containing multiple calculated values (e.g., return [3 => $sum];), passing by reference remains essential when the function needs to mutate external state or when working with legacy codebases that rely on this pattern.

A critical trade-off to consider when using references is side-effect management. Because you are directly modifying the input, any errors within the function will alter the original data structure in ways that might not be immediately obvious until the function scope ends. If $waffles contained complex objects or was shared across different parts of a large application, mutating it inside foo could lead to unintended consequences elsewhere. Always ensure that the caller expects the input array to be mutated before passing it by reference. This practice aligns with broader principles found in modern PHP development resources, such as those discussed on https://laravelcompany.com, where managing data immutability and state flow is crucial for scalable architecture.

Our network