Say if this is the code:
function bar() {
$result = array();
for ($i = 0; $i < 1000; $i++) {
$result[] = $i * 10;
}
return $result;
}
$ha = bar();
print_r($ha);
Is it not efficient to return a large array like that since it is “return by value”? (say if it is not 1000 but 1000000). So to improve it, would we change the first line to:
function &bar() {
that is, just by adding an & in front of the function name — is this a correct and preferred way if a large array is returned?
There is a lot of misunderstanding about how PHP handles variable storage. Since PHP is “copy on write,” there is no need to create a “reference” (actually a symbol table alias) in an effort to save space or time. In fact, doing so can hurt you. You should only create a reference in PHP if you want to use it as an alias to something. I ran both of your snippets, and it looks like the second actually uses more memory (though not by much).
By the way, an array of 1000 integers is tiny.