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:

Related posts:

  1. How to apply a function to every array element in PHP
  2. PHP 7 – Combined Comparison (Spaceship) Operator
  3. How to create Triangles using CSS
  4. Voting Functionality in a website

23 thoughts on “PHP double quotes vs single quotes”

  1. Pingback: PHP Character Conversion? | Seek-Answers

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

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

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

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

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

  5. Also note, that a string like this:

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

    whereas:

    $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 🙂

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

  7. Thanks for this post. I have been struggling with trying to use store and retrieve and echo an apostrophe (which is a single quote) and after searching high and low for a clue as to the problem, I happened upon your post which made clear what my problem was. Thanks for being a lifesaver.

  8. Thanks a ton. I was totally struggling with how to express strings in PHP and I could not find a post that could explain it for me simply.

Leave a Reply