• Skip to main content
  • Skip to primary sidebar
Martin Taylor

Martin Taylor

PHP, CSS, MySQL and WordPress tips and techniques

PHP echo performance revisited

Posted on May 8, 2020

PHP’s echo statement can be used with commas separating the elements to be output, or with dots. So either:

echo $a, $b, $c;
/* or */
echo $a . $b . $c;

I’ve usually preferred to use commas, because I figured that using a dot would cause PHP first to concatenate the items into a single string, and then output that. Seems like it would take longer, you’d think.

I’ve just seen a post at https://electrictoolbox.com/php-echo-commas-vs-concatenation/, apparently showing that I was right, and that using dots is slower than commas.

Thing is, this was posted many years ago, and I’m not sure what version of PHP the author was using.

I’ve just repeated the tests using the example code in that post, and on my local server running PHP 7.3, the results are different, and if anything, using dots is now quicker than using commas.

Using dots – elapsed time for the test was generally around 2.1 seconds

Using commas – elapsed time was pretty much always 2.3 seconds.

Now, for four million echos, that’s a negligible difference. I think I’ll stick with commas.

Filed under: PHP

Primary Sidebar

Recent Posts

  • PHP Calendar Class using relative dates
  • PHP Calendar Class revisited
  • Wildcard subdomains in XAMPP
  • MySQL – retrieving rows with one column as key
  • CSS-only menus in WordPress

Copyright Martin Taylor © 2023