Introduction
In PHP, arguments to a function can be passed by value or passed by reference. By default, values of actual arguments are passed by value to formal arguments which become local variables inside the function. Hence, modification to these variables doesn't change value of actual argument variable.
When arguments are passed by reference, change in value of formal argument is reflected in actual argument variable because the former is a reference to latter. Thus pass by reference mechanism helps in indirectly manipulating data in global space. It also helps in overcoming the fact that a function can return only one variable.
Pass by Value
In following example, two variables are passed to swap() function. Even though swapping mechanism takes place inside the function it doesn't change values of variables that were passed
Example
<?php function swap($arg1, $arg2){ echo "inside function before swapping: arg1=$arg1 arg2=$arg2\n"; $temp=$arg1; $arg1=$arg2; $arg2=$temp; echo "inside function after swapping: arg1=$arg1 arg2=$arg2\n"; } $arg1=10; $arg2=20; echo "before calling function : arg1=$arg1 arg2=$arg2\n"; swap($arg1, $arg2); echo "after calling function : arg1=$arg1 arg2=$arg2\n"; ?>
Output
This example gives following output
before calling function : arg1=10 arg2=20 inside function before swapping: arg1=10 arg2=20 inside function after swapping: arg1=20 arg2=10 after calling function : arg1=10 arg2=20
Pass by reference
In order to receive arguments by reference, variable used formal argument must be prefixed by & symbol. It makes reference to variables used for calling the function. Hence, result of swapping inside function will also be reflected in original variables that were passed
Example
<?php function swap(&$arg1, &$arg2){ echo "inside function before swapping: arg1=$arg1 arg2=$arg2\n"; $temp=$arg1; $arg1=$arg2; $arg2=$temp; echo "inside function after swapping: arg1=$arg1 arg2=$arg2\n"; } $arg1=10; $arg2=20; echo "before calling function : arg1=$arg1 arg2=$arg2\n"; swap($arg1, $arg2); echo "after calling function : arg1=$arg1 arg2=$arg2\n"; ?>
Output
Result of swapping will be shown as follows
before calling function : arg1=10 arg2=20 inside function before swapping: arg1=10 arg2=20 inside function after swapping: arg1=20 arg2=10 after calling function : arg1=20 arg2=10
In following example, array element are references to individual variables declared before array initialization. If element is modified, value of variable also changes
Example
<?php $a = 10; $b = 20; $c=30; $arr = array(&$a, &$b, &$c); for ($i=0; $i<3; $i++) $arr[$i]++; echo "$a $b $c"; ?>
Output
Values of $a, $b and $c also get incremented
11 21 31
It is also possible to pass by reference an array to a function
Example
<?php function arrfunction(&$arr){ for ($i=0;$i<5;$i++) $arr[$i]=$arr[$i]+10; } $arr=[1,2,3,4,5]; arrfunction($arr); foreach ($arr as $i) echo $i . " "; ?>
Output
Modified array will be displayed as follows
11 12 13 14 15
Object and reference
In PHP, objects are passed by references by default. When a reference of object is created, its reference is also sent as argument in the form of $this which is also reference to first object
Example
<?php class test1{ private $name; function getname(){ return $this->name; } function setname($name){ $this->name=$name; } } $obj1=new test1(); $obj2=&$obj1; $obj1->setname("Amar"); echo "name: " .$obj2->getname(); ?>
Output
Above code will display following output
name: Amar