PHP copies on write, so if the data doesn't change in the function, using a reference only makes things run slower.
In your case, you are changing the data, so a copy will occur. Test with the following:
<?php
define('N', 100000);
$data = range(1, N);
srand(1);
function ref(&$data)
{
$data[rand(1, N)] = 1;
}
function ret($data)
{
$data[rand(1, N)] = 1;
return $data;
}
echo memory_get_usage()."\n";
echo memory_get_peak_usage()."\n";
ref($data);
// $data = ret($data);
echo memory_get_usage()."\n";
echo memory_get_peak_usage()."\n";
?>
Run it once with ref()
and once with ret()
. My results:
ref()
- 8043280 (before / current)
- 8044188 (before / peak)
- 8043300 (after / current)
- 8044216 (after / peak)
ret()
- 8043352 (before / current)
- 8044260 (before / peak)
- 8043328 (after / current)
- 12968632 (after / peak)
So, as you can see, PHP uses more memory when modifying the data in the function and returning it. So the optimal case is to pass by reference.
However, passing by reference can be dangerous if it's not obvious that it is occurring. Often you can avoid this question altogether by encapsulating your data in classes that modify their own data.
Note that if you use objects, PHP5 always passes them by reference.