PHP Concatenation Benchmark – Comma vs Period

PHP provides many different ways to output text, such as echo and print. Echo and print does the same thing, although echo is slightly faster than print. When concatenating text in PHP, using commas (,) instead of periods (.) can lead to an increased performance.

To test out the difference in performance between echo and print, and the different concatenation operators. For each different output method, the script is ran 1000000 times. The following script is used to benchmark the different output methods.

$t = microtime(1);
$a = str_repeat('a', 100);
$b = str_repeat('b', 100);
$c = str_repeat('c', 100);
$d = str_repeat('d', 100);
while($i < 1000000) {
	/* Function here */
echo microtime(1) - $t . "n";

Echo with Comma – 1.1117630004883 seconds

echo $a, $b, $c, $d;

Echo with Period – 1.2538111209869 seconds

echo $a . $b . $c . $d;

Echo with Double Parenthesis – 1.3720679283142 seconds

echo "$a$b$c$d";

Print with Period – 1.3239979743958 seconds

print $a . $b . $c . $d;

Print with Double Parenthesis – 1.4582200050354 seconds

print "$a$b$c$d";


As you can see above, outputting text with a comma as the concatenating operator has the fastest result. The downside of using a comma is that you can only use it with echo, and the following script WON’T WORK. It will return an error.

$e = $a, $b, $c, $d;

In conclusion, it is the most efficient to use commas when echo-ing a statement, and stay away from double quotes whenever possible. It’s more efficient to use echo instead of print.

One thought on “PHP Concatenation Benchmark – Comma vs Period

  1. jkoudys

    Came across this old article from google – it’s important to note that you’re only echo/printing 4 variables together, which isn’t a very intensive test. You’ll find that the speed advantage of using the commas is much more evident when you have more variables.
    e.g. this:

    echo $a, $b, $c, $d, $a, $b, $c, $d,$a, $b, $c, $d, $a, $b, $c, $d,$a, $b, $c, $d, $a, $b, $c, $d,$a, $b, $c, $d, $a, $b, $c, $d;

    Was running at ~2.1s in my test case, while concatenating the same string (with .) runs in about 4.6s, a significant difference.

    While PHP devs are quick to balk at lower-level perf tests like this (with the knee-jerk response being to call everything a microoptimization), as the numbers above show this really can be significant. The comma operator is essentially processing everything in sequence, so echo sends the first variable to the output stream, then the second, third, etc. Using a concatenation, you’re actually saying “build one big string of all the values put together, then send to the output”. For a small number of variables, the cost of the echo itself is more than the , or . . For large sets of variables, it starts to add up considerably. If you’re just saying `echo ‘Hello’, ‘ ‘, ‘World!’`, then it probably doesn’t matter, but if you’re outputting a long paragraph full of variables, as shown above it can really add up. What’s probably more important than the raw execution time is memory usage – if you build a really big paragraph by concatenation, you’ll see it spike as it starts buffering that paragraph in memory, while using commas avoids that as your strings are going to output right away.

Comments are closed.