Has someone made a StringBuilder
implementation in PHP?

- 349,597
- 67
- 533
- 578

- 67,098
- 47
- 117
- 162
-
3Why would you want something like this? – Ben Nov 18 '10 at 18:38
-
5Related: [php String Concatenation, Performance](http://stackoverflow.com/q/124067/106224) (many of the answers do dismiss the need for such a class, but all with good reasons) – BoltClock Nov 18 '10 at 18:40
-
1You don't need StringBuilder in PHP, see my answer: http://stackoverflow.com/questions/124067/php-string-concatenation-performance/16112845#16112845 – nightcoder Apr 19 '13 at 20:19
5 Answers
Note:
This answer is from 2010, there might be stringbuilders that can improve the performance by now (judging from the comments below). I have not worked with php for a long time so my knowledge is not up to date. This answer might be out dated.
The following question might provide interesting information as well, all tough their conclusion seem to be the same.
php String Concatenation, Performance
Why do you want to use a StringBuilder? Strings in php are mutable. Therefore performance is not an issue.
Just build string like this
$string = "start";
$string .= "appended string";
$string .= "appended string";
etc.

- 13,028
- 11
- 47
- 73
-
12I would +1, but the concatenation operator is `.=`, not `+=` (`+=` is used for addition, which is a different operator from concatenation)... – ircmaxell Nov 18 '10 at 18:41
-
3oops, Thanks for pointing that out. To much used to java :P. Fixed it now. – Mark Baijens Nov 18 '10 at 18:44
-
-
2Yeah it's a pain :P. This error drives me crazy everytime i switch to php. – Mark Baijens Nov 18 '10 at 18:51
-
Than there is no such need over `StringBuilder` in php. Thanks for pointing out that... – Tarik Jul 07 '11 at 07:34
-
2This is so naive. You can't say this for sure; because you need to be familiar with php internal data structures. I haven't seen the source code, have you? – Javid Apr 12 '15 at 08:47
-
3For the record, strings in PHP is _not_ - in any way - as fast as a string buffer. – Olle Härstedt Oct 16 '15 at 14:37
-
Question: Is there any way to reserve 1000 bytes upfront, if you know your result will be large, even if you're constructing it ~4 characters at a time? – Paul Gilmore Sep 12 '18 at 23:27
You can use sprintf
which is only a basic version but requires no extra libraries, examples Follow
$String = "Firstname %s, lastname %s, Age %d";
echo sprintf($String,"Robert","Pitt",22);
And also handles type casting and position replacements:
$format = "The %2$s contains %1$d monkeys. That's a nice %2$s full of %1$d monkeys.";
sprintf($format, $num, $location);
All though i do like the look of jacob's answer :)
taker a look at the great functionality of t his function and its sister function here: http://php.net/manual/en/function.sprintf.php

- 56,863
- 21
- 114
- 161
-
4@Webnet Well it's good for strings that are defined elsewhere (such as in a translation file). Otherwise it becomes nothing more than a magic string (which is the same concept as a [Magic Number](http://en.wikipedia.org/wiki/Magic_number) ) – ircmaxell Nov 18 '10 at 18:39
-
L18n is exactly the best use for this function and also its sister function for char conversions: `printf("char 65 = %c", 65);` – RobertPitt Nov 18 '10 at 18:44
There are some implementations out there, however I don't see why you would need a StringBuilder in PHP,at least not for performance reasons. Plain string concatenation in PHP is faster than sprintf or the impelementation Jacob suggested.

- 13,296
- 6
- 55
- 83
You don't need StringBuilder or StringBuffer in PHP , PHP is super handy I offer you , using from hereDoc and NowDoc if you you wish to keep PyString :
$YourString = "start";
$YourString .= <<<'EOD'
appended string
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
$YourString .= <<<buffer
appended string
Example of string
spanning multiple lines
using heredoc syntax.
appended string
appended string
buffer;

- 461
- 5
- 12
-
based on your answer, I made a suggestion of using variables and class fields right inside of the HERE_DOC/NOW_DOC block. – Dimitry K Aug 15 '14 at 14:20
Answer of @Amir, gave me inspiration to the fact that in PHP if you want 'named parameters' or 'positional' parameters, you don't need sprintf
, but HERE_DOC/NOW_DOC works perfect. You can even use this inside of a class for properties and call getters.
class MyClass{
private $property;
private $stock; // some other object with getter 'getSomeProperty()'
function __toString(){
$localvar = 'Localvar';
$localvar2 = 'Localvar2';
return <<<HERE_DOC
{{
fqsn: {$this->stock->getSomeProperty()},
property: {$this->property},
localvar: {$localvar},
localvar2: $localvar2
}}
HERE_DOC;
} // end __toString()
} // end MyClass

- 2,236
- 1
- 28
- 37