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?
Any type may be returned, including arrays and objects.
Another option to consider is to use write parameters. Then, to call: $arr1 = array(); $arr2 = foobar($arr1); This won't be useful if you always need to return two arrays, but it can be used to always return one array and return the other only in certain cases.
An array is a special variable that we use to store or hold more than one value in a single variable without having to create more variables to store those values. To create an array in PHP, we use the array function array( ) . By default, an array of any variable starts with the 0 index.
If you want to return all values of that array, you need to push each value to a temporary storage variable and return that at the end of the loop: $output = array(); foreach ($myArray as $key => $value) { $output[] = $value; } return $output; That's for your problem.
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.
They consume absolutely the same amount of memory, because of COW
PS: to get "true" passing by reference you also need to add it in the assignment:
$ha =& bar();
If you throw in a memory_get_peak_usage()
in the function and outside, you'll see that returning the array does not increase the memory.
PHP copies on write. In this case, there is nothing to copy even if you do write because the original variable is out of scope. So no, there is no reason to do this.
And in fact, in PHP, there's generally no reason to use references unless you need the functionality that they provide: altering the original variable.
Official manual page says: Returning by reference is useful when you want to use a function to find to which variable a reference should be bound. Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so
http://it2.php.net/manual/en/language.references.return.php
So, in your case, don't use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With