As we discussed in previous e.g what is function passing by value ,in passing by value we can change value of variable by passing values , but if we want to change our global variable value than we use function by reference,in function by reference we pass parameters to function with the reference of variable not the value of variable as mention in below .
Example:
<?php
//declare gloabal variable
$myvar= "global variable";
// function to show the value
function myfunction(&$myparameter) {
$changeparameter = "global vaiable inside";
echo "This is $changeparameter the function<br />";
}
// call function
myfunction($myvar);
// print global variable value
echo "This is $myvar outside the function";
?>
In above example (&) with parameter use to set reference for variable,it will modify global variable value
No comments:
Post a Comment