7

Having used Java for a long time my standard method for creating long strings piece by piece was to add the elements to an array and then implode the array.

$out[] = 'a';
$out[] = 'b';
echo implode('', $out);

But then with a lot of data.

The (standard PHP) alternative is to use string concatenation.

$out = 'a';
$out .= 'b';
echo $out;

To my surprise there seems to be no speed difference between both methods. When there is significant time difference usually it is the concatenation that seems faster, but not all of the time.

So my question is: are there - apart from style and code readability - any other reasons to choose one approach over the other?

2
  • 1
    PHP ist not Java. So don’t be surprised that they differ. Commented Dec 4, 2009 at 14:54
  • In PHP, Strings are not immutable or even objects. Commented Dec 4, 2009 at 14:57

6 Answers 6

9

To me, using an array implies that you're going to do something that can't be done with simple string concatenation. Like sorting, checking for uniqueness, etc. If you're not doing anything like that, then string concatenation will be easier to read in a year or two by someone who doesn't know the code. They won't have to wonder whether the array is going to be manipulated before imploded.

That said, I take the imploded array approach when I need to build up a string with commas or " and " between words.

Sign up to request clarification or add additional context in comments.

Comments

4

Choose the more readable one. Always. This case, i would pick up the second apporach. Then optimize it, if it's a bottleneck.

3 Comments

No it is not a bottleneck, just curious. Anyhow I already mentioned readability.
I can only agree to this - never do something since a micro-benchmark shows it's 0.0001% faster. with XDebug or zendDebugger there are profilers for PHP and if that's not enough there are ways to profile the PHP runtime too, time spent there is way better used than in maintianing "optmized" code (and often it's even cheaper to run some more hardware than paying the developer)
+1 just choose the most appropriate one. If implode solves the problem elegantly, use it.
3

One (subtle) difference is clearly visible when generating a character-seperated string:

<?php
$out[] = 'a';
$out[] = 'b';
echo implode(',', $out);

foreach($out as $o) {
    echo $o . ',';
}

?>

The first one will print a,b where the latter will print a,b,. So unless you're using an empty string as a seperator, as you did in your example, it's usually preferred to use implode().

2 Comments

One could do: $first = true; foreach ($out as $o) { if ($first) $first = false; else echo ','; echo $o; } That should give you a,b instead.
@Raney: why all the trouble when implode() way is easier?
2

The concatenation-vs-implode holy war aside: No, there is no difference.

Comments

2

Here is a performance test for both approaches:

<?php

const M = 1000000;

// Imploding method
$start = microtime(true);
$arr = [];
for ($i = 0; $i < M; $i++) {
    $arr[] = chr(65 + ($i & 0x7));
}
$str1 = implode('', $arr);
$time1 = microtime(true) - $start;

// Concatenation method
$start = microtime(true);
$str2 = '';
for ($i = 0; $i < M; $i++) {
    $str2 .= chr(65 + ($i & 0x7));
}
$time2 = microtime(true) - $start;

assert( $str1 == $str2 ); 
echo "Time 1: $time1\n";
echo "Time 2: $time2\n";

Output with PHP 7.1.33 on MacOS:

Time 1: 0.121246
Time 2: 0.059228

Output with PHP 8.0.5 on a (slow) Debian:

Time 1: 0.88076496124268
Time 2: 0.8109610080719

So, concatenation method works faster.

Comments

0

it depends on what you want to do with the string / array and how you create it

if you start with an array and need to sort it / manipulate certain elements, then i suggest implode

other than that i usually use concatenation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.