Last Update - 10 Nov 2018

Call by reference in php with example

Call by reference in php with example

Let's understand the concept of call by reference by the help of examples. Variable $str is passed to the adder function where it is concatenated with 'Call By Reference' string. Here, printing $str variable results 'This is Call By Reference'. You can pass a variable by reference to a function so the function can modify the variable.

function adder(&$str2)  
{  
    $str2 .= 'Call By Reference example';  
}  
$str = 'This is ';  
adder($str);  
echo $str;  

 

Output:

This is Call By Reference

 

Another example with nunber:

function increment(&$i)  
{  
    $i++;  
}  
$i = 5;  
increment($i);  
echo $i;  

 

Output:

6

More For You

Did you find this page helpful? X