Call by reference in php language with complete example

Call by reference in php language with complete 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:

Loading...
6

Related posts

Write a comment