Search code examples
phparraysreturn-by-referencereturn-by-value

Is returning an array by reference from a method more efficient?


I have a function that returns an array. In PHP arrays are returned by value by default, unless the & operator is used. I assume when returning an array from a function by value results in a copy of the array being created. Is it therefore more efficient to return a reference to the array?


Solution

  • It appears, according to the PHP documentation on Returning References, that the answer is 'No':

    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.