views:

6422

answers:

9

In languages like Java and C#, strings are immutable and it can be computationally expensive to build a string one character at a time. In said languages, there are library classes to reduce this cost such as C# System.Text.StringBuilder and Java java.lang.StringBuilder.

Does php (4 or 5; I'm interested in both) share this limitation? If so, are there similar solutions to the problem available?

A: 

no such limitation in php, php can concatenate strng with the dot(.) operator

$a="hello ";
$b="world";
echo $a.$b;

outputs "hello world"

paan
That's not what the question is asking....
Swati
people here is quick on the trigger.. i was typing in the dark.. accidentally hit tab then enter..
paan
+1  A: 

Yes. They do. For e.g., if you want to echo couple of strings together, use

echo str1,str2,str3 

instead of

echo str1.str2.str3 
to get it a little faster.

mixdev
does this function work this way?$newstring = str1.srt2.str3;echo $newstring;
JoshFinnie
+14  A: 

No, there is no type of stringbuilder class in PHP, since strings are mutable.

That being said, there are different ways of building a string, depending on what you're doing.

echo, for example, will accept comma-separated tokens for output.

// This...
echo 'one', 'two';

// Is the same as this
echo 'one';
echo 'two';

What this means is that you can output a complex string without actually using concatenation, which would be slower

// This...
echo 'one', 'two';

// Is faster than this...
echo 'one' . 'two';

If you need to capture this output in a variable, you can do that with the output buffering functions.

Also, PHP's array performance is really good. If you want to do something like a comma-separated list of values, just use implode()

$values = array( 'one', 'two', 'three' );
$valueList = implode( ', ', $values );

Lastly, make sure you familiarize yourself with PHP's string type and it's different delimiters, and the implications of each.

Peter Bailey
+1  A: 

PHP strings are mutable. You can change specific characters like this:

$string = 'abc';
$string[2] = 'a'; // $string equals 'aba'
$string[3] = 'd'; // $string equals 'abad'
$string[5] = 'e'; // $string equals 'abad e' (fills character(s) in between with spaces)

And you can append characters to a string like this:

$string .= 'a';
yjerem
A: 

Firstly, if you don't need the strings to be concatenated, don't do it: it will always be quicker to do

echo $a,$b,$c;

than

echo $a . $b . $c;

However, at least in PHP5, string concatenation is really quite fast, especially if there's only one reference to a given string. I guess the interpreter uses a StringBuilder-like technique internally.

Anthony Williams
A: 

If you're placing variable values within PHP strings, I understand that it's slightly quicker to use in-line variable inclusion (that's not it's official name - I can't remember what is)

$aString = 'oranges';
$compareString = "comparing apples to {$aString}!";
echo $compareString
   comparing apples to oranges!

Must be inside double-quotes to work. Also works for array members (i.e.

echo "You requested page id {$_POST['id']}";

)

cori
+6  A: 

When you do a timed comparison, the differences are so small that it isn't very relevant. It would make more since to go for the choice that makes your code easier to read and understand.

SeanDowney
Indeed, worrying about this is just outright silly, when there are usually far more important issues to worry about, like database design, big O() analysis, and proper profiling.
DGM
That is very true, but I HAVE seen situations in Java and C# where using a mutable string class (vs. s += "blah") have indeed increased performance dramatically.
Pete Alvin
A: 

$a = "Hello ";

$b = "World";

(a) $c = $a.$b;

(b) $c = "$a$b";

I would like to know which one is faster to concat (a) or (b). OR if there is another faster way to contact, let me know.

zawmn83
I haven't checked it, but I think (a) is faster because (b) need to parse the string to check if it contains variables in it (in your case it does).
mixdev
+1  A: 

I know what you're talking about. I just created this simple class to emulate the Java StringBuilder class.

class StringBuilder {

private $str = array();

public function __construct() { }

public function append($str) {
    $this->str[] = $str;
}

public function toString() {
    return implode($this->str);
}

}

purlogic