Re: Return Values Copied? Copied if By Reference?
| From: | Adam Zey | Date: | Thu, 27 Jul 2006 18:05:22 +0000 |
| Subject: | Re: Return Values Copied? Copied if By Reference? | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-239843@lists.php.net to get a copy of this message | ||
Robert Cummings wrote:
On Thu, 2006-07-27 at 01:35, Larry Garfield wrote:Then how come when I do a foreach on an array (without modifying anything within the foreach), it still makes a copy of the array that consumes memory? I think it's dangerous to generalize that it's always best to let PHP make copies of things. In the foreach situation, the preferred solution when memory is a problem is to either use a reference, or have foreach iterate over the keys of the array. Regards, Adam.On Wednesday 26 July 2006 21:41, Robert Cummings wrote:Nope. You should only use references if you really need them. Attempting to improve efficiency by using references instead of copies when you aren't actually in need of a reference will result in less efficiency since the engine spends more time creating the reference than the copy. Cheers, Rob.By the same token, then, if I have a function that generates a large string and returns it, is there any benefit to return-by-reference?I'm working on some code that would be called to generate a cell in a possibly large table and therefore a small difference in performance may have a significant impact.PHP uses copy-on-write and so copies are essentially shared until such time as you modify one of them. If you don't need references then copies are faster than references.