What’s the difference between call by value & call by reference in PHP? This is the most important concept used in programming and sometime asked in interviews. Don’t worry if you are new to this terminology, you’ll learn this concept in this tutorial.
Call by Value & Call by Reference in PHP
Example 1: Let’s understand this concept using some programming examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * Passed value using call by value */ function increment($num) { $num = $num + 1; echo $num."\n"; } $n = 1; increment($n); /* Output 2 */ echo $n; /* Output 1 */ |
Example 2: Now again write this script with minimal modification in an argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * passed value using call by reference. */ function increment(&$num) { $num = $num + 1; echo $num."\n"; } $n = 1; increment($n); /* Output 2 */ echo $n; /* Output 2 */ |
We have seen the examples and their differences in output. Let’s understand what exactly is call by value & call by reference.
Call by Value in PHP
In call by value, the value of a variable is passed directly. It means, if the value of a variable within the function is changed, it doesn’t get changed outside of the function. As we seen in our first example.
Call by Reference in PHP
In call by reference, the address of a variable (their memory location) is passed. In the case of call by reference, we prepend an ampersand (&) to the argument name in the function definition. Any change in variable value within a function can reflect the change in the original value of a variable which we have seen in our second example.
Magic methods in PHP
Swap Two Numbers using Call By Value in PHP
In call by value, the actual value of a variable is passed. In this example, we have swap two numbers using call by value.
PHP code – Find first non-repeated character in a string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/** * Swap two numbers using call by value * @param type $first * @param type $second */ function swap($first, $second) { $temp = $first; $first = $second; $second = $temp; echo "First number ".$first." Second number ".$second."\n"; } $a = 5; $b = 7; /* Output : First number 7 Second number 5 */ swap ($a, $b); /* Output : First number 5 Second number 7 original value is not changed even after calling swap method. */ echo "First number ".$a." Second number ".$b; |
Swap Two Numbers using Call By Reference in PHP
In call by reference, Address of a variable is passed. Address represents where the value of a variable is stored. In this example, we swap two numbers using call by reference in PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/** * Swap two numbers using call by reference * @param type $first * @param type $second */ function swap(&$first, &$second) { $temp = $first; $first = $second; $second = $temp; echo "First number ".$first." Second number ".$second."\n"; } $a = 5; $b = 7; /* Output : First number 7 Second number 5 */ swap ($a, $b); /* Output : First number 7 Second number 5 */ echo "First number ".$a." Second number ".$b; |
In the above example, we have passed the value of $a = 5 and $b = 7 in swap() method. In swap() method, we are passing the value by reference. It means the address of a variable is passed. So any change in the value of variables in a swap() will reflect them in original values of a variable.
For further you can check PHP documentation.