Single quotes or double quotes for variable concatenation?

54,547

Solution 1

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)

Solution 2

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;

Solution 3

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

Solution 4

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.

Share:
54,547

Related videos on Youtube

ina
Author by

ina

moody. perchance bipolar. i'm even crazier on answers.unity3d.com and a few other stackX's :)

Updated on July 09, 2022

Comments

  • ina
    ina almost 2 years

    Is it better to concatenate a variable (say, $name) into an existing string (say, $string) like this:

    $string='Hi, my name is '.$name
    

    or to embed the variable in the string like this:

    $string="Hi, my name is $name";
    

    or is it better to use a function like this:

    $string=sprintf("Hi, my name is %s",$name);
    

    Which is better in terms of processor time/efficiency?

  • ina
    ina almost 14 years
    "everyone who did the test..." data source?
  • Blizz
    Blizz almost 14 years
  • snowflake
    snowflake almost 14 years
    I think test is even not required. It is just a matter of number of processor instructions. The less you have (that can be easily guessable in this case), the faster you are.
  • Gumbo
    Gumbo almost 14 years
    The parsing is just done once. There is no additional parsing for double quoted strings.
  • Fidi
    Fidi almost 14 years
    Of course the parsing is done just once. But the the steps within the parsing-proccess are meant. With double quotes the parser has to add the step of variable-interpolation to its parsing-chain.
  • Marc B
    Marc B almost 14 years
    Your array example works just fine without the concatenation. ... from a {$text['random']} array, or ... from a $text[random] array. You'd only have to use the {} method if you're referencing a multi-dimensional array, as PHP's parser isn't greedy. $array[x][y] will get parsed as <contents of $array[x]><plain text [y]>.
  • Martin Bean
    Martin Bean almost 14 years
    Yes. But I find using braces in strings just gets a bit ugly to me. Like I say, it's just an example of how I personally concatenate strings with variables.
  • Josh
    Josh over 13 years
    I disagree that "Hi, my name is $name" is "old", and that 'Hi, my name is '.$name is somehow better!
  • RobertPitt
    RobertPitt over 13 years
  • alexia
    alexia over 13 years
    Another way: echo $str1, $str2;
  • user2864740
    user2864740 over 8 years
    If there was ever a relevant performance reason it makes no difference now - nikic.github.io/2012/01/09/…
  • Bojoer
    Bojoer over 7 years
    This is BS. There is maybe only a very slight performance difference between using double and single quotes. Difference between the two is that the parser will use variable-interpolation in double-quoted strings, but not with single-quoted strings. So maybe in very early days this could have been a performance difference but as told here in this answer above never a MARGINALLY difference. Resources are coding guidelines FIG / PSR php-fig.org/psr and github.com/zikula/core/wiki/… What are your sources for this information you gave?
  • Blizz
    Blizz over 7 years
    Dude.. seriously? Not only are you responding to an answer that is more than 6 years old, you are basically saying exactly the same thing. That is what "marginally better" means: "very slight".