PHP double quotes vs single quotes

PHP double quotes vs single quotes

Strings in PHP can be specified in four different ways: single quoted, double quoted, heredoc syntax and (since PHP 5.3.0) nowdoc syntax, the first two of them being by far the most frequently used.

It is important to know the difference between using single quotes and double quotes. In this post we will see the difference between them and which should be used when.

Single quoted strings are the easiest way to specify string. This method in used when we want to the string to be exactly as it is written. When string is specified in single quotes PHP will not evaluate it or interpret escape characters except single quote with backslash (‘) and backslash(\) which has to be escaped.

echo 'This is \'test\' string';
//Output: This is 'test' string

In double quoted strings other escape sequences are interpreted as well any variable will be replaced by their value.

$count = 1;
echo "The count is $count";
//Output: The count is 1

If we use single quotes instead of double quotes for the above example it will be like this:

$count = 1;
echo 'The count is $count';
//Output: The count is $count

I recommend using single quotes (‘ ‘) for string unless we need the double quotes (” “). This is because double quotes forces PHP to evaluate the string (even though it might not be needed), whereas string between single quotes is not evaluated. Also, parsing variables between strings takes more memory than concatenation.

So instead of this:

$count = 1;
echo "The count is $count";

use this:

$count = 1;
echo 'The count is ' . $count;
Related Articles:

  1. Very informative! I’ve been using PHP for over 5 years and never bothered looking into the differences between single and double quotes.

  2. Okay you just made my with this.
    I knew there was the whole “$var”=value system, but I didn’t know that ‘quotes’ are more efficient since they need less memory and work. Thatnks a lot I already rewrote our page and finally I don’t need to break the string each time I need to put the same type of quote inside itself. ๐Ÿ™‚

    But I’d do like to ask one thing. I had a few errors when I changed everything and then I learned that things like URL-s need to be between “quotes”(I just finished my university in informatics, but minor things like these were left out in class).
    So I’ds like to ask if “quotes” are used within ‘quotes’ those are not checked by PHP, but used by HTML in the browser right?
    Because then some of the inner ‘quotes’ could be changed to “quotes” instead of escaped by \. At least at parts where ‘quotes’ aren’t required.

  3. I’d recommend interpolation over concatenation for two reasons:
    1. It’s more readable
    2. It’s less error-prone. In your example above you made this exact error, since the two strings are not equivaltent – there’s a space missing in the concatenation version.

  4. thanks. i got the differents between single quotes and double quotes in php

  5. Which is faster? Single quote or double quote?

    • Single quotes should be faster than double quotes. Unless you have very complicated string with lots and lots of concatenations. But in most cases single quotes will be faster.

  6. I’m not sure if winning a micro second is really what we can call faster ๐Ÿ˜‰

    • We might be saving only a micro second in small applications or when we don’t have lot of statements with double quotes. However, in large applications or when using lot of similar statements, we can potentially save lot of micro seconds or even a few seconds. Also, when we want to have our application be as fast as it can even a micro second can help.

  7. Also note, that a string like this:

    $mailbody = ‘I want a line break:\nDone.’; // is parsed showing the \n


    $mailbody = “I want a line break:\nDone.”; // will give you the line break

    A mistake that actually everybody is running in, once in a while ๐Ÿ™‚

  8. Thanks for putting this together. I always use single quoted because on my keyboard (swiss-german) it has its own key compared to double-quoted where I need to press shift+2. I only learned later the differences between those.

Leave a Reply

Trackbacks and Pingbacks: