PHP echo with commas versus concatenationPHP echo with commas versus concatenation

Posted August 30th, 2007 in PHP

The language constructs "echo" and "print" are used in PHP for writing strings out to a stream, such as a web browser via a web server, or to the console when PHP is run as a command line script. The two work in a very similar way but for all intents and purposes are the same (read this post at FAQTS for the differences between the two).

I have always used concatenation when writing out multiple strings with echo or print, but recently discovered that echo accepts multiple arguments or parameters, comma separated like a regular function call.

For example, when using concatenation, you would do this:

echo $string1 . $string2;
print $string1 . $string2;

When using commas with echo, you would do this:

echo $string1, $string2;

Note that you can only do this with echo and not with print; if you try to use comma separated values with print you will get a syntax error message.

Having learnt this, I decided to do some benchmarking to see if it is any faster using the comma variation or normal string concatenation, assuming the comma version would be slightly faster. In my tests, I first joined two 100 character strings four million times, and then three 100 character strings four million times. The reason for doing it so many times was to get large enough time values to make a meaningful comparison.

This survey was not particularly scientific: I ran each test a number of times and worked out a rough average in my head. However, the numbers produced were different enough in each iteration to clearly see that one method was faster than another.

Joining two 100 characters strings four million times:

echo $string1, $string2;
Approx average 1.48 seconds

echo $string1 . $string2;
Approx average 1.62 seconds

print $string1 . $string2;
Approx average 1.67 seconds

Joining three 100 characters strings four million times:

echo $string1, $string2, $string3;
Approx average 2.08 seconds

echo $string1 . $string2 . $string3;
Approx average 3.48 seconds

print $string1 . $string2 . $string3;
Approx average 3.52 seconds

So, as I expected, echo using commas is marginally faster than echo with concatenation, and echo with concatenation is marginally faster than print with concatenation. In the real world, the difference between the three would be infintisimal: it's only when we loop through thousands of times or more that we can see and measure any real difference.

Below is the benchmarking code I used to conduct this test. I needed to split it into an outer and inner loop with ob_get_clean() in the inner loop, otherwise the process would consume too much memory and error out. For example, the Apache log files said "PHP Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 8396801 bytes)" when I tried to loop through too many iterations without cleaning the buffer. I could simply increase the allowable memory for the purposes of the test but having two loops was easy enough to do.

$string1 = str_repeat('x', 100);
$string2 = str_repeat('y', 100);
$string3 = str_repeat('z', 100);

$mark = microtime(true);

for($i = 0; $i < 2000; $i++) {
    
    ob_start();
    for($j = 0; $j < 2000; $j++) print $string1 . $string2 . $string3;
    ob_get_clean();

}

echo microtime(true) - $mark;

Comments

blog comments powered by Disqus