Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PHP, how do we return a large array?

Tags:

php

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?

like image 643
Jeremy L Avatar asked May 02 '12 00:05

Jeremy L


People also ask

Can you return an array in PHP?

Any type may be returned, including arrays and objects.

How can I return multiple arrays in PHP?

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.

What is array[] PHP?

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.

How do you return all values in an array?

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.


4 Answers

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.

like image 136
Explosion Pills Avatar answered Oct 20 '22 08:10

Explosion Pills


They consume absolutely the same amount of memory, because of COW

  1. http://ideone.com/bZgm7
  2. http://ideone.com/e3Jfr

PS: to get "true" passing by reference you also need to add it in the assignment:

$ha =& bar();
like image 28
zerkms Avatar answered Oct 20 '22 09:10

zerkms


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.

like image 3
Matthew Avatar answered Oct 20 '22 10:10

Matthew


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.

like image 1
dAm2K Avatar answered Oct 20 '22 09:10

dAm2K