Passing by Reference

In PHP, the & symbol, when prefaced to a variable, tells the parser to pass a reference to the variable’s value, not the value itself. Consider the following example, in first function the value is not modified by the called function, but in second function (call by reference) the called values is modified easily.

// pass by value
function pass_by_value($param) {
  $param = $param+5;


$var = 1;
// In function value by call variable will note be chaged
// in the called block
echo "variable 1 : " . $var ."<br>";

// In function by reference the passed value can be modified
// in the called block

$var2 = 5;
echo "variable 2 : " . $var2 ."<br>";

// & is used to pass by reference
function pass_by_reference(&$param) { 
  $param = $param + 5;


You may also like...