Strings: best practice?

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!

Moderator: General Moderators

Post Reply
aos24
Forum Newbie
Posts: 9
Joined: Tue Feb 06, 2007 4:37 pm

Strings: best practice?

Post by aos24 »

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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Strings are variables so yes. Whether you use substr() or $str[] does not matter -- they are for you to store string data and manipulate however you like.
(#10850)
aos24
Forum Newbie
Posts: 9
Joined: Tue Feb 06, 2007 4:37 pm

Post by aos24 »

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?)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Do some testing.
aos24
Forum Newbie
Posts: 9
Joined: Tue Feb 06, 2007 4:37 pm

Post by aos24 »

Thanks, but how am I supposed to reliably determine what the garbage collector is doing?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Well, the most reliable way is to look at the internal code that would be run.. but since that's a rats' nest... Profilers may offer information, as may using crude checks in PHP.
aos24
Forum Newbie
Posts: 9
Joined: Tue Feb 06, 2007 4:37 pm

Post by aos24 »

I could do that, but I was hoping someone here would just know, and would be able to say either, "Yes, updating strings in place is considered good practice", or, "Don't do that: it will work but you'll create lots of temporary strings for the garbage collector to clean up!"
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

aos24 wrote:Thanks, but how am I supposed to reliably determine what the garbage collector is doing?
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.

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)
aos24
Forum Newbie
Posts: 9
Joined: Tue Feb 06, 2007 4:37 pm

Post by aos24 »

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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

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)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

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";
?>
#1 65796
#2 65844
#3 131456
#4 131504
#5 197052
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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It looks like str_replace() and the string position changes use about the same amount of memory. Or am I reading that wrong?
aos24
Forum Newbie
Posts: 9
Joined: Tue Feb 06, 2007 4:37 pm

Post by aos24 »

Thanks, volka, that was very helpful. Great FAQ, too.
Post Reply