PHP Passing by Reference

PHPServer Side ProgrammingProgramming

PHP Online Training

45 Lectures 9 hours

PHP in Telugu

43 Lectures 5.5 hours

Redis and PHP

22 Lectures 40 mins

Introduction

In PHParguments 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

Live Demo

<?php
function swap($arg1,$arg2){
echo "inside function before swapping: arg1=$arg1 arg2=$arg2";
$temp=$arg1;
$arg1=$arg2;
$arg2=$temp;
echo "inside function after swapping: arg1=$arg1 arg2=$arg2";
}
$arg1=10;$arg2=20;
echo "before calling function : arg1=$arg1 arg2=$arg2";
swap($arg1,$arg2);
echo "after calling function : arg1=$arg1 arg2=$arg2";
?>

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

Live Demo

<?php
function swap(&$arg1, &$arg2){
echo "inside function before swapping: arg1=$arg1 arg2=$arg2";
$temp=$arg1;
$arg1=$arg2;
$arg2=$temp;
echo "inside function after swapping: arg1=$arg1 arg2=$arg2";
}
$arg1=10;$arg2=20;
echo "before calling function : arg1=$arg1 arg2=$arg2";
swap($arg1,$arg2);
echo "after calling function : arg1=$arg1 arg2=$arg2";
?>

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

Live Demo

<?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

Live Demo

<?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)

Example

Live Demo

<?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
Updated on 18-Sep-2020 12:13:27