Single quotes or double quotes for variable concatenation? [closed]

Everyone who did the test concluded that using single quotes is marginally better performance wise. In the end single quotes result in just a concatenation while double quotes forces the interpreter to parse the complete string for variables.

However the added load in doing that is so small for the last versions of PHP that most of the time the conclusion is that it doesn't really matter.

So for the performance people: use single quotes. For the "i like my code readable"-people: double quotes are a lot better for the legibility, as Flavius Stef already pointed out.

Edit: One thing though - If you are going to use a a single dollar in your string without a variable, use single quotes for sure! (http://www.weberdev.com/get_example-3750.html points out that it will take 4 times longer to parse those strings)


The difference between single and double quotes in PHP is that double quotes are "intelligent" in that they will parse for variables when being read, while single quotes are "dumb" and will not try to parse any character in the string.

These result in some minor differences in what characters you can use; basically, the only character you need to escape when using single quotes is a single quote itself:

'\''

While if you use double quotes you have to escape other characters:

"\$"

But it also allows for some nifty things like adding a new-line to the end:

"my string\n"

With single quotes you would have to do a concatenation:

'my string' . chr(10)
'my string' . "\n"

Generally, single quotes are faster because they are "dumb".

However, normally one should not really worry about these issues, that is called Premature optimization, and should be avoided.

A couple of words about optimization: generally one should first write the program the way it should work, and then find the biggest bottlenecks and fix those particular ones. If string speed really is an issue for you in PHP, you might want to consider switching to another language.

Regarding speed: you probably want to focus more on memory usage than on CPU time. In these cases the CPU time could be considered pretty constant. CPU time is more relevant when writing algorithms that will iterate many times.

Regarding concatenations: the more you concatenate strings using the dot-operator, the more memory you will be using.

Consider this:

$str1 = 'asdf';
$str2 = 'qwer';

// this will result in more memory being allocated for temporary storage
echo $str1 . $str2;

// this will not allocate as much memory as the previous example
echo $str1;
echo $str2;

I generally feel that using string interpolation ("Hi, my name is $name") is better from a legibility standpoint.


For performance, as others have proven, it is marginally faster to use single quotes rather than double quotes.

Single quotes, if applied to readability science and kept away from subjectivity actually adds more "noise". Noise and how it relates to readability is talked a lot about in the book Clean Code and one could conclude that the more non-whitespace you have to see, the more it hinders readability. If applied to subjectivity, most places that I've taken the time to read actually prefer single over double quotes.

Use your judgement.

$var = "My $string with $lots of $replacements."

Is much more readable than:

$var = 'My ' . $string . ' with ' . $lots . ' of ' . $replacements . '.';

I'll admit that:

$var = "My string.";

Looks almost the same as:

$var = 'My String.';

However the latter introduces less noise and when there's lots of code around it every little bit helps, not to mention the other benefits you get from using single quotes.

In the end, I prefer to KISS. Use single quotes unless you need double quotes. Simple convention that is easier to type, easier to maintain, easier to parse and easier to read.