Concatenating strings or using multiple echo parameters: which is faster?

Suppose I have two PHP statements:

echo "foo"."bar"

echo "foo", "bar"

Notice the different ways of concatenating the strings - using a . or a , .

I realise the actual differences between the two methods, that using , gives multiple parameters to the keyword echo , whereas using . actually joins the strings together before echo ing.

But my question is, which way is faster?


Aotoki's answer is untrue. The double quoted strings can contain a variable. The commas and dots have nothing to do with variables versus literal strings.

When using echo (a "language construct") multiple parameters can be declared. When doing so, you can concatenate with dots or commas. There is a slight difference in their behavior because of "precedence"; this is an issue that explained in the php manual (see earlier link to manual).

Many benchmark tests have been run over the years on this topic, and everyone that I have ever seen has stated that comma concatenation is faster than dot concatenation. For this reason, I never use dots to concatenate with echo.

Here are some links that offer some actual benchmark results:

  • https://stackoverflow.com/a/17437821/2943403
  • https://www.electrictoolbox.com/php-echo-commas-vs-concatenation/
  • https://www.fusionswift.com/2010/05/php-concatenation-benchmark-comma-vs-period/
  • All this said, this topic is about micro-optimization. If you have a project that truly need to shave time because it is impacting users, chances are there is a bigger fish to fry than echo concatenation. In virtually all instances, your end user is not going to have the slightest clue if you used commas or dots to echo your content.

    链接地址: http://www.djcxy.com/p/59074.html

    上一篇: 在FOR循环中迭代FOREACH数组

    下一篇: 连接字符串或使用多个回显参数:哪个更快?