The difference between using single quotes and double quotes as string delimiters in JavaScript/PHP

I like to keep all my string delimiters in languages such as PHP and JavaScript consistent. I consistently use single quotes for a couple of good reasons, so when I encounter someone else’s script that uses double quotes the OCD in me makes me change them all to single quotes…but that is a different problem.

If you’re coming from a language such as C# or Java then note that in PHP and JavaScript there is no such thing as a character datatype – as such, you can use either single or double quotes to delimit strings.

If I could only give you one reason why I consistently use single quotes then I would have to go with this:

If you ever (God forbid) need to write HTML in a string, you don’t need to escape the double quotes around attribute values. Instead of:

    "<div id=\"foo\" class=\"bar\">foobar</div>"

You write:

    '<div id="foo" class="bar">foobar</div>'

…which is lots less characters to type and I’d say more readable. Also, I more frequently use double quotes in my JavaScript/PHP strings than single quotes so it really does make sense.

The other reason is that if you use double quotes, PHP will actually parse the contents of the string to find variables and escape characters (\n, \r, \t etc.) which it’ll replace with the variable value or correct escape character respectively. So for example:

<?php

    $baz = 'bar';
    echo "foo\n$baz";

?>

Will print:

foo
bar

Whereas:

<?php

    $baz = 'bar';
    echo 'foo\n$baz';

?>

Will print:

foo\n$baz

So if you don’t need any of this functionality in your string, you’re making PHP do some extra work for no reason.

8 thoughts on “The difference between using single quotes and double quotes as string delimiters in JavaScript/PHP

  1. Have you ever considered writing an ebook or guest authoring on other sites?
    I have a blog centered on the same subjects you discuss and would really like to have you share some stories/information.
    I know my visitors would enjoy your work. If you are even remotely interested, feel free to shoot me an
    e mail.

  2. [... ]however websites we backlink in order to listed here are significantly not in connection with our bait, many of us feel they can be actually worth a chance by way of, and so do a look[... ]!

  3. In years 5 10, hazards reversed and mortality approximated to women in background population after 5 years in patients with core basal cancers and after 10 years in patients with HER2E breast cancer cialis dosage Biophotonics 13

  4. Colon cancer is one of the cancers in the world which is very common 55 56 how to buy priligy im 16 years old E 2 BSA 10 9 M stimulates NO release with the same kinetic profile as 17 ОІ estradiol p ОІ estradiol and E 2 BSA stimulated NO release were antagonized by tamoxifen Tam

  5. 28 In various anti doping regulations this provision apart from other rights that the athlete may claim is found buy priligy online usa Cathet Cardiovasc Diagn 12 426 429

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>