Screen Printing

TUTORIALS » PHP / MySQL » PHP essentials (guided tutorials)

Single quotes vs Double quotes in PHP



One of the eternal debates in PHP is whether is best to use single quotes or double quotes to define a string. Here I will try to explain the basic pros/cons of each one.

Strings with double quotes


PROS

  • You can insert variables directly within the text of the string. The PHP parser will automatically detect such variables, convert their values into readable text, and place them in their proper places (this is called concatenating, funny word).

    Example:
    <?php
     $name = "Peter";
     $age = 42
     echo "Hi, my name is $name and I am $age years old.";
    ?>
    The example above will render on the screen:
    Hi, my name is Peter and I am 42 years old.
    The variables were automatically detected and concatenated with the text.

CONS

  • You cannot write a text string which includes double quotes without using the escape slash. Let me explain. Since you have to start your string with a double quote, you cannot write another double quote inside the string; that would cause the valid string to close.

    Example.
    <?php
     echo "My friends call me "The Party Monster" all the time";
     $site = "google.com";
     echo "<a href="http://$site/">Click here</a>";
    ?>
    In the last example, both echo lines will cause PHP errors. In the first one I "accidentally" closed the string with the first quote of "Party Monster" and everything else is left outside the valid string. In the second line I closed my string before the URL even began so, again, everything else is left outside the string.

    The workaround to include valid double quotes inside double-quoted strings is to use the escape slash. The escape slash is a (\) symbol which indicates PHP that the character right next to it should be taken as a special character.

    So, the proper way to write the last example using double quotes would be:
    <?php
     echo "My friends call me \"The Party Monster\" all the time";
     $site = "google.com";
     echo "<a href=\"http://$site/\">Click here</a>";
    ?>
    Now the echo lines will render properly, without errors.

    At first, this issue doesn't seem to be such a big deal, but when you have to code a big web site full of HTML parameters enclosed in double quotes, the task can be quite annoying.

  • PHP takes longer to process double quoted strings. Since the PHP parser has to read the whole string in advance to detect any variable inside—and concatenate it—it takes longer to process than a single quoted string.

    This can be negligible for small sites with a few short strings; but if you need to process a big web site, with heavy loads of traffic and large strings to parse, the resources of your server can be significantly decimated.

Strings with single quotes


PROS

  • The text appears as it is. When it comes to enclosing strings in PHP, single quotes are much stricter than double quotes. When you enclose a string with single quotes, PHP ignores all your variables. Plus, you are able to write double quotes inside the text without the need of the escape slash.

    Example:
    <?php
     $name = 'David';
     echo 'Hi, my name is $name "The Crazy" Jones.';
    ?>
    The example will render:
    Hi, my name is $name "The Crazy" Jones.
    The variables are ignored and the double quotes are respected!

    Now, how can you concatenate variables with a string using single quotes? By using the dot notation.

    The following example shows you how:
    <?php
     $name = 'David';
     echo 'Hi, my name is ' . $name . ' "The Crazy" Jones.';
    ?>
    The dot acts as a bond between properly quoted pieces of a string and the variables we want to concatenate (notice that the variables are left unquoted).

  • Single quotes are easier on the server. Since PHP does not need to read the whole string in advance, the server can work faster and happier.

Cons

  • Some people complain about the dot notation being too elaborated; personally I prefer it over the escape slashes every time I want to include HTML parameters in my strings—something I do a lot.

Comments & Questions