xxxxxxxxxx
// Pass by reference
// This can be useful when you need to change/update the value of the arguments. We cannot return more than one value from function. So, we use pass by
// reference because when we swipe two numbers in a function and want to return two values from function it is not possible without pass by reference.
function first($num){ // Pass by Value
$num+=5;
// echo $num; // In function $num = 15 but outside the function without return $num, the value of $number is 10.
}
function second(&$num){ // Pass by Reference we pass the address of $number to the formal parameter ($num).
$num+=7;
// echo $num; // In function $num = 15 and outside the function without return $num, the value of $number is 17.
}
$number = 10;
first($number);
echo "Pass by Value ". $number. "<br>"; // 10
second($number);
echo "Pass by Reference ". $number; // 17
function swap(&$a,&$b){
$temp = $a;
$a = $b;
$b = $temp;
// we cannot return two values. i.e a and b. so that we use pass by refernce.
}
$x = 3;
$y = 4;
swap($x,$y);
echo "Value of x : " .$x. "<br>";
echo "Value of y : " .$y;