By Craig Buckler

High-Performance String Concatenation in PHP

By Craig Buckler

We recently examined the complexities of fast string concatenation in JavaScript so I thought it would be useful to repeat the exercise for PHP. This could be a more important to your application: large string operations are often handled on the server when generating HTML pages.

There are no special libraries in the standard PHP installation and string concatenation is implemented using the dot operator:

$str = 'a' . 'b';
$str .= 'c';

You can also join an array of strings using the implode function (or it’s alias, join):

$str = implode(array('a', 'b', 'c'));

If you’re only joining a few strings, you should use whichever method is most practical. Readability and functionality is always more important than negligible performance gains.

Concatenating many strings

Consider the following functionally identical examples. The first uses a string concatenation operator:

// standard string append
$str = '';
for ($i = 30000; $i > 0; $i--) {
	$str .= 'String concatenation. ';

The second uses an array join:

// array join
$str = '';
$sArr = array();
for ($i = 30000; $i > 0; $i--) {
	$sArr[] = 'String concatenation. ';
$str = implode($sArr);

Which is fastest?

The good news is that PHP5 is quick. I tested version 5.3 and you’re far more likely to run out of memory than experience performance issues. However, the array implode method typically takes twice as long as the standard concatenation operator. A comparable period of time is required to concatenate the string or build the array, but the implode function doubles the effort.

Unsurprisingly, PHP is optimized for string handling and the dot operator will be the fastest concatenation method in most cases.

  • Antonio Kobashikawa Carrasco

    Thank you for your post.
    I think (IMHO) that time for array creation should not be considered part of time for string concatenation in test with implode method.
    Or to be more fair, it could be compared with time for:
    $str = ”;
    $sArr = array();
    for ($i = 30000; $i > 0; $i–) {
    $sArr[] = ‘String concatenation. ‘;
    for ($i = 30000; $i > 0; $i–) {
    $str .= $sArr[i];

  • apaatsio

    I modified the benchmarks a little to suit better the situations I deal with daily. I didn’t measure the array creation time and I used for loop starting from zero up. It doesn’t make sense to me to test the time to append a static string so I used the array in the standard string append method, too. I also tested with foreach. Major thing is that I replaced implode($sArr) with implode(”, $sArr) which tripled the speed! Tested with PHP 5.3.0 without any opcode cache.

    Here are my results (time in milliseconds):
    standard: 11.67
    implode: 1.89
    foreach: 3.99

    Here’s my code. (I run it on Drupal which has the timer_start and timer_stop methods.)

    $sArr = array();
    for ($i = 0; $i < 30000; $i++) {
    $sArr[] = 'String concatenation. ';

    // standard string append
    $str = '';
    for ($i = 0; $i < count($sArr); $i++) {
    $str .= $sArr[$i];
    $t1 = timer_stop('standard');
    print "standard: " . $t1['time'] . "n";

    // array join
    $str = implode('', $sArr);
    $t2 = timer_stop('implode');
    print "implode: " . $t2['time'] . "n";

    // foreach
    $str = '';
    foreach($sArr as $string) {
    $str .= $string;
    $t3 = timer_stop('foreach');
    print "foreach: " . $t3['time'] . "n";

  • As we say in french : CQFD.
    Aka Ce Qu’il Fallait Démontrer (What Had to Be Demonstrated).

    • Louis Simoneau

      The English equivalent is QED, from the latin Quod Erat Demonstrandum, which means exactly that :)

      • Q.E.D.

        I thought Q.E.D. was latin for “In your face!” :-)

  • Maik

    some time ago I saw somebody using PHP streams (php://memory, php://temp) for massive string concatenation. He just appended the strings to the stream, did a rewind and read the whole result from the stream. I’m quite not sure about the performance. Maybe you should give it a try.

  • zet

    I think some time ago someone already did that comparisons and it turned out the fastest way for concatenating strings in PHP is to use output buffering and echos, something like:
    for ($i = 30000; $i > 0; $i–) {
    echo ‘string concatenation’;
    $string = ob_get_clean();

  • I find this kind of micro-optimisation a bit worthless to be honest. If your application is running slow, changing the way you concatenate string isn’t going to help. It’s far better to make the code readable than waste time and effort trying to get the odd millisecond here and there out of the code for no real benefit.

  • Duckie

    Todo for the PHP world: Stop posting benchmarks for micro optimizations.
    I’ll repeat it for the 1000th time: If you have to wory about string performance in PHP, you’re doing it *WRONG*.

  • Pete Shaw

    Who gives a ****? seriously.

  • James

    It would be obvious that the string concatenation is faster by just reading the code. I am going on memory so I might be in correct. In Big-O this would be N for string concatenation.

    If you read the second code sample with the implode function. It would be obvious that implode must loop through the array again. N squared for the second sample code.

    • It’s not as obvious as you might think. String concatenation can be extremely memory-inefficient and an implode loop is significantly faster than your own looping code.

      Take a look at the JavaScript comparision — an array join is 800x faster in IE7.

  • toastyghost

    what about…

    echo $a,$b,$c;
    $str = ob_get_contents();


    i know using the 1…n echo parameters is an order of magnitude faster than the string concatenation operator, but i haven’t found anything about the resource overhead of using the output buffer. anyone?

  • impressthenet

    It’s good to see commentors mention ob_start() and related functions. In my experience, these are not used that often. However they can be a very useful tool. For instance, I am in the process of recoding an open source (non-MVC) app to fit within the template of the site. The site’s templating structure is a simple one which simply outputs within the main div the content of a global variable. So a quick solution to is to selectively use ob_start(), ob_clean(), ob_get_content() and ob_end_clean(). Any print or echo statement won’t get output to the browser before I intent it to.

    As to the question at hand, I’m not sure about the performance of output buffering in relation to string concatenation. Personally, I’ve utilized heredoc notation for YEARS.

  • Jacky
    • I think you mean single-quotes. Double quotes are only required if you have variables embedded in the text; they take longer to parse.

      • Golgote

        You’re wrong.

        Your benchmark is also wrong. Concatenation should be faster, you didn’t measure correctly.

      • Golgote

        I mean implode…

  • Dr.Colossos

    This might be a micro-optimization, but only for small output.

    I ran in the very same problem when I had to output huge (several thousand lines of HTML) documents – it was for a project managment application. Think of forms that allow editing a whole project with 1000 sub-tasks, each with 50 data members -> 50.000 form fields! you might argue that this is too much data to display, but it was a customer requirement. Try argue this …

    Basic string concatination was far slower than output buffering. Output buffering was about as fast as writing to the phpmemory and phptemp streams, which is similar to SPLTempFile object.

    So, for HUGE documents, it sure does matter.

  • chenry1674

    Thanks for the breakdown. However, I have to say, whenever I read something like this, I always think that developers should be spending their time on better things. Never in my career have I seen a performance problem where the solution was to change the way I concatenate strings. If your app is running slow, find the bottleneck. If it’s string concatenation, I will be extremely, extremely surprised.

Get the latest in Front-end, once a week, for free.