Pass by Value and Pass by Reference in PHP

PHP supports two kinds of methods for passing arguments to functions: pass by value and pass by reference. Passing an argument by value is the most common way to pass value to a function.

In PHP, arguments are passed by value by default. When you pass arguments by value, PHP makes a copy of the variable’s value and sends it to the function.

Any changes made to the variable’s value inside the function do not affect the original variable outside the function. When the function exits, the copy is destroyed. Let’s take an example to understand this concept.

Example 1:

<?php
function increment($num) {
// Adding 1 to the copy of the variable.
   $num += 1;
   echo "Inside function: \$num = $num\n";
}

$value = 5;
increment($value);
echo "Outside function: \$value = $value\n"; // Original value is unchanged.
?>
Output:
      Inside function: $num = 6
      Outside function: $value = 5

In this example, when we have passed an argument by value to increment() function, a copy is made of $num and passed to the function. Inside the function, we have made a change by adding 1 to it and the variable $num outside the function is not changed.


So, when we called the function, it displays the updated value. But, the variable $value remains unchanged outside the increment function because only a copy of $value was modified.

Pass by Reference in PHP


If you want to modify the original value of a variable inside the function, you can pass the variable by reference in PHP. To achieve it, use the & symbol before the parameter name in the function definition. Passing by reference allows you to override the default rule and provides a function to direct access to the variable’s value.

Remember that only variables can be passed by reference. Therefore, an argument must be a variable. When a variable is passed by reference, the function works directly with the original variable, not with a copy.

Any changes made to it inside the function will directly affect the original variable throughout the script. Let’s take an example to understand the concept of pass by reference in PHP.

Example 2:

<?php
function increment(&$num) {
// Adding 1 to the original variable.
   $num += 1;
   echo "Inside function: \$num = $num\n";
}

$value = 5;
increment($value); // Passing the variable by reference.
echo "Outside function: \$value = $value\n";
?>
Output:
      Inside function: $num = 6
      Outside function: $value = 6

In this example, the increment() function directly modifies the value in the original variable $value since the $num parameter in the function definition was passed by reference (using the & symbol).


The change inside the function is reflected outside as well because both the parameter $num inside the function and the variable $value outside the function point to the same memory location.

Example 3: Pass by Reference with Array

<?php
function doubleValues(&$arr) {
   foreach ($arr as &$value) {
      $value *= 2;
   }
}

$numbers = [1, 2, 3, 4];
// Passing an array by reference.
doubleValues($numbers);
print_r($numbers);
Output:
      Array
     (
        [0] => 2
        [1] => 4
        [2] => 6
        [3] => 8
      )

Difference Between Pass by Value and Pass by Reference


AspectPass by ValuePass by Reference
DefinitionA copy of the variable is passed.The actual variable is passed.
SyntaxGeneral syntax is used in PHP.Requires  to use & before the parameter in the function definition.
Effect on Original VariableNo change to the original variable.Any changes affect the original variable.
PerformanceIt is slightly slower for large data.It is faster for large data because no copying is made.
Use CaseThis method is useful for read-only operations where the original variable should remain unchanged outside the function.This method is particularly useful when the function needs to update the value of the original variable.

In this tutorial, you have learned about the basic difference between pass by value and pass by reference in PHP with the help of examples. I hope that you will have understood both concepts in PHP.