2

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 echoing.

But my question is, which way is faster?

Jacob Garby
  • 613
  • 6
  • 17

1 Answers1

3

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:

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.

mickmackusa
  • 33,121
  • 11
  • 58
  • 86