Call by Reference in PHP with Examples
In PHP, the default method for passing arguments to a function is call by value, meaning changes inside the function don’t affect the original variable. However, PHP also supports call by reference, which allows a function to modify the original variables directly.
Let’s explore call by reference in PHP, how it works, and see examples that make it easy to understand.
Step 1: What is Call by Reference?
Call by reference allows a function to work with the actual variable, not just a copy. By creating a reference, PHP enables two variables to share the same value. Any change to one variable will affect the other.
Example: Reference Variable
<?php
$var = "Hello";
$var1 = &$var;
$var1 = "Hello World";
echo "var = $var, var1 = $var1 \n";
$var = "How are you?";
echo "var = $var, var1 = $var1 \n";
?>
Output:
var = Hello World, var1 = Hello World
var = How are you?, var1 = How are you?
Here, $var1 is a reference to $var. Changing either $var or $var1 reflects in both, as they share the same memory location.
Step 2: Calling a PHP Function by Reference
To pass a variable by reference in PHP, simply prefix the argument with an & in the function declaration.
Syntax:
function change_value(&$arg) {
// Function body
}
When you pass a variable by reference to this function, changes made inside the function will affect the original variable outside of it.
Example: Call by Reference in a Function
<?php
function change_name(&$nm) {
echo "Initially the name is $nm \n";
$nm = $nm."_new";
echo "This function changes the name to $nm \n";
}
$name = "John";
echo "My name is $name \n";
change_name($name);
echo "My name now is $name";
?>
Output:
My name is John
Initially, the name is John
This function changes the name to John_new
My name now is John_new
The function change_name() modifies $name directly because it’s passed by reference using &$nm. As a result, the original variable $name is updated outside the function.
Step 3: Swapping Two Variables Using Call by Reference
One practical use of call by reference is swapping the values of two variables.
Example: Swapping Values by Reference
<?php
function swap_ref(&$a, &$b) {
echo "Initial values a = $a, b = $b \n";
$temp = $a;
$a = $b;
$b = $temp;
echo "Swapped values a = $a, b = $b \n";
}
$x = 10;
$y = 20;
swap_ref($x, $y);
echo "x = $x, y = $y";
?>
Output:
Initial values a = 10, b = 20
Swapped values a = 20, b = 10
x = 20, y = 10
By passing $x and $y by reference, the function swap_ref() successfully swaps the values, which are reflected outside the function as well.
Step 4: Returning a Value by Reference
PHP also allows functions to return a reference to a variable. This enables the returned variable to act as a reference to the original, allowing changes to affect both.
Example: Returning by Reference
<?php
function &myfunction() {
static $x = 10;
echo "Inside function, x = $x \n";
return $x;
}
$a = &myfunction();
echo "Returned by Reference: $a \n";
$a = $a + 10;
$a = &myfunction();
?>
Output:
Inside function, x = 10
Returned by Reference: 10
Inside function, x = 20
The function myfunction() returns a reference to the static variable $x, allowing changes to $a to reflect back on $x.
Step 5: Benefits of Call by Reference
Efficient Memory Usage: Instead of copying large variables, call by reference lets you work with the original data, saving memory.
Direct Modifications: It’s useful when you need to change the original variable’s value within a function.
Easier Management: Call by reference simplifies scenarios like swapping values or manipulating large data structures.
Call by reference is a powerful feature in PHP that allows functions to directly modify variables passed to them. By understanding the difference between call by value and call by reference, you can choose the appropriate method for your code, ensuring better memory efficiency and more control over variable manipulation.
Keep Learning 🙂