74

What is the difference between the two following methods (performance, readability, etc.) and what do you prefer?

echo "Welcome {$name}s!"

vs.

echo "Welcome " . $name . "!";
1
  • 4
    Whichever is easier to read.... but as this question is simply polling opinion rather than a question with a definitive or factual answer, then it isn't really appropriate as a SO question Commented May 28, 2013 at 11:09

1 Answer 1

62

Whatever works best for you works... But if you want to go for speed use this:

echo 'Welcome ', $name, '!';

The single quotes tell PHP that no interpretation is needed, and the comma tells PHP to just echo the string, no concatenation needed.

Sign up to request clarification or add additional context in comments.

9 Comments

The speed difference between single and double quotes is a myth (at least with versions of PHP released this millenium) phpbench.com
My reference to the speed was by using the comma instead of the dot, which is (marginally) faster. And that's also backed by the link you provided. Very interesting link btw!!!
The performance difference is literally microseconds! On the other hand echo "Welcome $name!"; is many times more readable.
@Shayne It's quite a jump from string interpolation in SQL being a bad, bad habit (I wholeheartedly agree!) to string interpolation in general being a bad habit. I personally find it more readable (less messy) than concatenation, and use it wherever applicable. Are there any strong arguments against string interpolation in general?
@shayne The SQL argument is completely irrelevant to the question, string concatenation is exactly as bad as interpolation, and for exactly the same reasons, in an SQL context.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.