PHP Call By Reference


 

PHP Call By Reference

In this tutorial we will study about call by reference, in which we pass the reference of the variable and modify the values inside another function.

In this tutorial we will study about call by reference, in which we pass the reference of the variable and modify the values inside another function.

PHP Call By Reference:

In PHP we can pass reference of a variable, so that we can modify the variable's value inside another function and get the modified values from where we have sent. It is much like the call by reference, in which we send the base address of the variable and we modify the values inside another function.

PHP Call By Reference Example:

<?php

function swap(&$a,&$b){

$a+=$b;

$b=$a-$b;

$a=$a-$b;

}

$var1=34;

$var2=90;

echo "<b>Before swapping values are \$var1= $var1 & \$var2= $var2 <br/> </b>";

swap($var1,$var2);

echo "<b>After swapping values are \$var1= $var1 & \$var2= $var2 <br/></b>";

?>

Output:

Before swapping values $var1= 34 & $var2= 90
After swapping values $var1= 90 & $var2= 34

 

Ads