Please wait

Passing by Reference

In PHP, "pass by value" is the default way of handing over information to a function. When you use a function in PHP and give it some information (or "arguments"), it's a lot like handing over a piece of paper with a message written on it. By default, PHP makes a copy of that piece of paper. This is called "passing by value." Even if the function changes the message (modifies the argument), your original piece of paper stays the same.

function changeValue($number) {
  $number = 100;
$myNumber = 5;
changeValue($myNumber); // $myNumber is still 5

It's like you gave the function a copy of your piece of paper, so even if the function scribbles all over it, your original message is safe.

In this example, the $myNumber variable retains its value 5 even if we call the changeValue function where the number changes because the function's $number parameter is a unique copy of the value.

What is passing by reference?

But what if you want the function to change the original piece of paper? That's where "passing by reference" comes in. Instead of giving the function a copy of your paper, you're allowing it to change the original one.

You do this in PHP by adding an & symbol before the argument in the function:

function changeValue(&$number) {
  $number = 100;
$myNumber = 5;
changeValue($myNumber); // Now, $myNumber is 100

In this case, the function didn't get a copy. It got to edit the original paper, so when it changed the number to 100, that change stuck even outside of the function.

Why use passing by reference?

"Passing by reference" is a powerful tool in PHP that can be used for several reasons:

  • Modify Original Variable: The primary reason for using pass-by reference is to allow a function to modify the original variable's value. This is useful when you want a function to update a value that should be reflected outside of the function's scope.
  • Performance Optimization: When you're dealing with large amounts of data (like big arrays), copying all that data (which is what happens when you "pass by value") can use a lot of memory and slow down your script. By passing the reference, you avoid creating a copy of the data, which can lead to more efficient, faster code.
  • Maintain Consistent References: Sometimes, you want multiple parts of your code to all work on the same data. Passing by reference ensures that everyone's working with the same original data, not copies that can become out-of-sync.

Here's an example of how you might use it:

function addFruitToBasket(&$basket, $fruit) {
    $basket[] = $fruit;
$myBasket = [];
addFruitToBasket($myBasket, 'Apple');
addFruitToBasket($myBasket, 'Banana');
// Now $myBasket contains ['Apple', 'Banana']

In this example, the addFruitToBasket function is able to modify the original $myBasket variable because the basket is passed by reference. If we passed by value, the $myBasket array would remain empty because only a copy would be modified inside the function.

Key takeaways

  • When passing by reference, a function can directly modify the original variable's value, allowing changes to be reflected outside the function's scope.
  • Passing by reference avoids creating a copy of the data, which can be particularly beneficial when dealing with large amounts of data, resulting in better memory usage and performance.
  • By passing by reference, multiple parts of the code can work on the same data, ensuring that everyone operates on the original value rather than creating separate copies.
  • To pass by reference, you prepend an ampersand (&) to the parameter in the function declaration and the function call.


Please read this before commenting