Share on Google+Share on Google+

PHP Call By Reference

Advertisement
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

 

Advertisements

Advertisement

Posted on: February 22, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.