What is the difference between in php function
- A parameter passage by variable
- A parameter pass by reference?
The best way to understand it is from an example:
function foo($a) { $a = 123; echo "Value in function: " . $a; } function bar(&$a) { $a = 123; echo "Value in function: " . $a; } $var = 555; foo($var); echo "After foo: " . $var; bar($var); echo "After bar: " . $var; Basically you will change the value pointed by the reference, changing it also out of the function scope, while in a normal by-value when the function is finished the changes made to the variable will be lost. Here is an official PHP manual link, with more examples.
A parameter passage by value - value of a variable is passed.
$b = 1; function a($c) { $c = 2; // modifying this doesn't change the value of $b, since only the value was passed to $c. } a($b); echo $b; // still outputs 1 A parameter pass by reference? - a pointer to a variable is passed.
$b = 1; function a($c) { $c = 2; // modifying this also changes the value of $b, since variable $c points to it } a($b); echo $b; // outputs 2 now after calling a();
valuenot byvariable. This is also covered all over the place. At least try to look first.