PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have a function which returns large blobs of HTML and I just switched it to return by reference via a parameter just for a cleaner looking invocation...
I just profiled the two numerous times and apparently there is little difference so I assume PHP5 returns all strings by reference already??? I know objects, etc are by default reference in PHP5...but what about when returning via return???
Anybody have a link or can U confirm my suspicions?
If i'm not mistaken they've choosen to store a string only once.. So it's perfectly possible that a couple of variables point to the same memory (that holds the string). So instead of passing the actual string i would presume that they only pass the address...
Btw, i think that your example would make more sense if you showed the signature (with the &).. since calling such a function is exactly the same as calling one without references...
timvw wrote:If i'm not mistaken they've choosen to store a string only once.. So it's perfectly possible that a couple of variables point to the same memory (that holds the string). So instead of passing the actual string i would presume that they only pass the address..
Yeah, I'm pretty sure that is what happens. The Zend engine avoids physically moving data around whenever it can send addresses. I'm pretty sure this will even be done with integers and floats as well because the zval (is that right?) is always a larger object than a memory address.
PHP is copy-on-write. It will never make a new copy of a variable in memory, right up until the point you modify it. So when you return the value it's the same location in memory, but if you then concatenate to it or some other operation, you'll see memory shoot up.