Passing And Returning References

Passing And Returning References

Basically references are used to create alias for other variables.
For example
$var1 = &$var2;

Here $var1 is the alias of $var2

Passing And Returning References
Following example will show how to pass and return the references

function func1(&$i){
return ++$i;
}

$j = 6;
func1($j);

function &func2($i)
{
global $j;

$j = $i;
return $j;
}

$k = 8;
$l = & func2($k);

$j = 9;
echo $l;

function func1(&$i) - This line shows how to pass reference to a function

func1($j) - This line calls the above function. In the above function argument is named as &$i.So this is an alias for passing variable.so if we change $i, it will affect $j.

function &func2($i)- This is example of how to return reference to a function.
$l = & func2($k); - This line calls above function. In the above function we are returning a global variable $j.So $l is as alias for $j.if we change $l, it will affect $j and of-course reverse is also true.

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.