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.

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

1
2
3
$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:

1
2
3
$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:

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

use this:

1
2
$count = 1;
echo 'The count is ' . $count;
  1. Thanks for any other excellent article. Where else may anyone get that kind of information in such an ideal method of
    writing? I’ve a presentation subsequent week, and I am at the search for such information.

  2. Thank you for the good writeup. It in fact was a
    amusement account it. Look advanced to far added agreeable from you!
    By the way, how could we communicate?

  3. I all the time used to study piece of writing in news papers but now
    as I am a user of net therefore from now I am using net for articles, thanks to web.

  4. I think this is among the most important information for me.
    And i am glad reading your article. But should remark on few general things,
    The website style is perfect, the articles is really nice : D.
    Good job, cheers

  5. Howdy! I’m at work surfing around your blog from my new iphone 4!

    Just wanted to say I love reading your blog and look forward to all your posts!

    Carry on the great work!

  6. drug store says:

    I really like what you guys are usually up too. This type of clever work and coverage!

    Keep up the superb works guys I’ve incorporated you guys to my personal blogroll.

  7. Amazing blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple adjustements
    would really make my blog shine. Please let me know where you
    got your design. Cheers

  8. Have you ever considered publishing an e-book or guest authoring on other sites?
    I have a blog based on the same subjects you discuss and would really like to have you share some stories/information. I know my subscribers would appreciate
    your work. If you’re even remotely interested, feel free
    to shoot me an e-mail.

  9. Brenda says:

    You share interesting things here. I think that your blog can go viral easily, but you must give it initial boost and i know how to do
    it, just type in google – mundillo traffic increase

  10. India says:

    Wow, incredible weblog structure! How lengthy
    have you ever been blogging for? you made running a
    blog look easy. The overall look of your website is great, as well as the content!

*