I would avoid using reference variables, if you can. There are times when they are useful, but more often than none they cause confusion and make the code harder to read.
Instead of:
Code: Select all
function changeValue(&$val) {
$val = 345;
}
$aValue = 123;
changeValue($aValue);
var_dump($aValue); // prints 'int(345)'
Why not do it the traditional way:
Code: Select all
function changeValue($value) {
$value = 345;
return $value;
}
$aValue = 123;
$aValue = changeValue($aValue);
var_dump($aValue); // prints 'int(345)'
See, in the second example, it is quite apparent that the value of $aValue is being changed--in the first example you have no idea. Look what happens If you were to rename the function name to something a little more obscure:
Code: Select all
//Which is clearer?
// This?
$aValue = 345;
zzz($aValue);
// Or this?
$aValue = 345;
$aValue = zzz($aValue);