Strings: best practice?
Moderator: General Moderators
Strings: best practice?
I know that it's possible to update a character of a string in place -- e.g. $message[4] = 'T' -- but is this considered best practice? Are PHP strings mutable? (If not, would the interpreter create a separate copy of the entire $message string when the above code is executed?)
Thanks.
Thanks.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Thanks. I don't think you can assign to substr() -- I get "Can't use function return value in write context" when I try it.
So would, say, updating several hundred characters in a 1-4K string using $message[$i] = $newchar be reasonably efficient? (I.e., it wouldn't create hundreds of copies of $message for the garbage collector to deal with?)
So would, say, updating several hundred characters in a 1-4K string using $message[$i] = $newchar be reasonably efficient? (I.e., it wouldn't create hundreds of copies of $message for the garbage collector to deal with?)
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
In general you should not be concerned with "what the garbage collector is doing", only that it is doing it. If you have such extreme memory management issues then you would probably build a C extension to PHP to deal with it or use a language where you manage you own memory allocation.aos24 wrote:Thanks, but how am I supposed to reliably determine what the garbage collector is doing?
Though I honestly can't imagine that you actually need to be concerned about memory management. Plus the time and cost of doing so would cost be far, far more than a few bucks for more memory.
(#10850)
I really don't want to get down and dirty with the garbage collector! What I want to do is to establish good coding practices for strings, so that if there are several comparable approaches to a programming task I can choose the one that will wreak the least havoc under the covers. Once I've done that, I'll be happy to code away without worrying too much about memory management.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
The problem with trying to "wreak the least havoc under the covers" in a language like PHP is that how thing work under the covers can and does change from release to release. So unless you have some very critical memory issue and are going to do thorough testing each release you use then it is just not worth it. PHP has an extensive library of string functions for string manipulation -- I would recommend seeing if there is something there that does what you need first.
(#10850)
Code: Select all
<?php
$start = memory_get_usage();
$f = str_repeat('-', 65536);
echo '#1 ', memory_get_usage() - $start, "\n";
$a = $f;
echo '#2 ', memory_get_usage() - $start, "\n";
$a = str_replace('-', '+', $f);
echo '#3 ', memory_get_usage() - $start, "\n";
$b = $f;
echo '#4 ', memory_get_usage() - $start, "\n";
$b[4] = 'x';
echo '#5 ', memory_get_usage() - $start, "\n";
?>Adding a simple new reference to the string does not increase the memory usage much. But changing the string does. The "old" unchanged value is still accessible via $f. str_repeat and $b[4]= create a new string. String are immutable in php.#1 65796
#2 65844
#3 131456
#4 131504
#5 197052
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
It looks like str_replace() and the string position changes use about the same amount of memory. Or am I reading that wrong?