I can’t find anything official, but in my opinion, the Zend Engine will only copy the array when trying to write to it. That is, there is no copy in this following code:
function printFirst($array) { echo $array[0]; } $array = range(1,1000); printFirst($array);
However, a copy will be made in this code if no other optimizations are made (i.e., this example function does nothing, so it can be completely eliminated).
function changeFirst($array) { $array[0] = 101; } $array = range(1,1000); changeFirst($array);
So, to answer your question, no, passing by reference will not make any difference. If you really need to change the original, then of course you need a link anyway.
Notice, I found a duplicate question here: In PHP (> = 5.0) is the link faster? And here: How does PHP assign and free memory for variables?
Update
This is the code I ran that demonstrated the significant memory usage of the object, although not as much as the array itself (about 2/3).
<?php class A { public $data; public function __construct($data) { $this->data = $data; // slight change $this->data[0] += 1; } }; $baseUsage = memory_get_usage(); $array = range(1,1000); $arrayUsage = memory_get_usage(); echo "Array took: ", $arrayUsage - $baseUsage, "\n"; $a = new A($array); echo "Object took: ", memory_get_usage() - $arrayUsage; ?>
source share