Categories
php string syntax

What is the difference between single-quoted and double-quoted strings in PHP?

892

I’m a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes.

I just know in .NET, or the C language, if it is in a single quote, that means it is a character, not a string.

0

    1194

    PHP strings can be specified not just in two ways, but in four ways.

    1. Single quoted strings will display things almost completely “as is.” Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\ (So yes, even single quoted strings are parsed).
    2. Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let’s say you have the variable $type and you want to echo "The $types are". That will look for the variable $types. To get around this use echo "The {$type}s are" You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such.
    3. Heredoc string syntax works like double quoted strings. It starts with <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don’t need to escape quotes in this syntax.
    4. Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.

    Notes:
    Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:

    $string = 'He said "What\'s up?"';
    $string = "He said \"What's up?\"";
    

    Speed:
    I would not put too much weight on single quotes being faster than double quotes. They probably are faster in certain situations. Here’s an article explaining one manner in which single and double quotes are essentially equally fast since PHP 4.3 (Useless Optimizations toward the bottom, section C). Also, this benchmarks page has a single vs double quote comparison. Most of the comparisons are the same. There is one comparison where double quotes are slower than single quotes.

    10

    • 32

      +1 for the curly brace trick. Wasn’t aware of that. Too bad it doesn’t follow the same conventions as shell scripts, i.e. ${variablename}.

      – devios1

      Jul 25, 2013 at 0:26


    • 7

      I only use double quotes, when i need it for \n, anything else in my PHP code is in single quotes.

      – Jo Smo

      Jul 4, 2014 at 13:26

    • seems ‘heredoc’ is ported to PHP from bash or is it not? Anyways great answer, so a +1 Thanks.

      – sjsam

      Jan 8, 2015 at 6:20

    • 6

      Interesting note in PHP documentation comments: php.net/manual/en/language.types.string.php#120160 – “The double-quoted strings “which look so $slow since they have to parse everything for \n backslashes and $dollar signs to do variable expansion”, turned out to be the FASTEST string concatenation method in PHP – PERIOD! Single-quotes are only faster if your string is completely literal (with nothing to parse in it and nothing to concatenate), but the margin is very tiny and doesn’t matter.”

      – dregad

      Aug 10, 2017 at 9:45

    • 3

      Note: slowness of " double quotes is all but a thing of the past. Updates have increased processing of double quotes to be as fast, in all but extreme cases, these days.

      Jan 19, 2019 at 20:45

    242

    Things get evaluated in double quotes but not in single:

    $s = "dollars";
    echo 'This costs a lot of $s.'; // This costs a lot of $s.
    echo "This costs a lot of $s."; // This costs a lot of dollars.
    

    3

    • 1

      Escaped single quotes and escaped backslashes are expanded even in single quoted strings.

      Aug 10, 2010 at 5:34

    • 50

      A mistake that many developers new to PHP are running in: $mailbody = 'I want a line break:\nDone.'; is keeping the \n alive. Whereas: $mailbody = "I want a line break:\nDone."; will parse the line break.

      – Avatar

      Apr 18, 2014 at 14:25


    • 2

      My two cents is needed only for interviews or for malware development. Just compare var_dump() from two expressions: $testWithAsciiAndUtf8Characters = "\x48\x41\x4c\114\117 \u{0147}\u{012B}\u{0144}\u{014D}!"; $simpleTest = '\x48\x41\x4c\114\117 \u{0147}\u{012B}\u{0144}\u{014D}!'; Character sequences in $testWithAsciiAndUtf8Characters were transformed to string with real letters.

      Nov 20, 2018 at 11:26


    69

    ' Single quoted

    The simplest way to specify a string is to enclose it in single quotes. Single quote is generally faster, and everything quoted inside treated as plain string.

    Example:

    echo 'Start with a simple string';
    echo 'String\'s apostrophe';
    echo 'String with a php variable'.$name;
    

    " Double quoted

    Use double quotes in PHP to avoid having to use the period to separate code (Note: Use curly braces {} to include variables if you do not want to use concatenation (.) operator) in string.

    Example:

    echo "Start with a simple string";
    echo "String's apostrophe";
    echo "String with a php variable {$name}";
    

    Is there a performance benefit single quote vs double quote in PHP?

    Yes. It is slightly faster to use single quotes.

    PHP won’t use additional processing to interpret what is inside the single quote. when you use double quotes PHP has to parse to check if there are any variables within the string.

    4

    • The one exception to single quotes NOT parsing anything within the string, is what you can use \' to escape a single apostrophe for use within the string (or \\' to display the backslash). Note that traditional escape sequences, such as \n will NOT get parsed to the newline character. PHP docs on strings

      Jan 19, 2019 at 20:48

    • I see on all of the answers that: you can use the variable’s name inside a double quote to print out the variable’s value, but you’re saying, the variable name should also be enclosed within curly braces to avoid the need for using a concatenating period. So if both “$my_var” and “{$my_var}” output $my_var’s value, what are the braces for? Thanks!

      – Bahman.A

      Dec 10, 2019 at 21:27


    • 1

      @Bahman.A I know it’s been years and you probably already know the answer by this point but in case others are looking in future for the same thing: The curly branches are added when vars are used in double quotes primarily for readability of the code. Highly useful when quickly scanning to grok it is a var and not a string literal.

      Aug 11, 2021 at 12:43

    • @Bahman.A I believe that the reason why you would use "{$one}" instead of "$one" isn’t readability but instead for exact variable lookup. { } also allows for array and object lookups. The dot operator, used for concatination of strings, would still be required for a function lookup

      – Justin L

      Feb 8 at 8:57