922

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.

Manuel Mannhardt
  • 2,191
  • 1
  • 17
  • 23
rob waminal
  • 18,117
  • 17
  • 50
  • 64

7 Answers7

1222

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 escape sequences (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". 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:
There is no difference.
Please read a credible article on the matter from one of PHP core developers. Speaking of tests, one should never take them for granted. It must be understood that writing a credible test and, especially, interpreting its results requires a lot of knowledge and experience. Which means that most tests out there are just bogus. For example, in a code like this

for($i=0;$i<100000;$i++) {
    'string';
}

The quoted string gets parsed only once, along with entire script, and then gets translated into opcode. Which is then gets executed a million times. So it measures anything but parsing. And that's only a tip of the iceberg. For a nanobenchmark like this, it's practically impossible to create a credible test that wouldn't be spoiled by some interfering side effect.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
  • 33
    +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 '13 at 00: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 04 '14 at 13:26
  • seems 'heredoc' is ported to PHP from bash or is it not? Anyways great answer, so a +1 Thanks. – sjsam Jan 08 '15 at 06:20
  • @sjsam - looks like it - neat, didn't know that: http://tldp.org/LDP/abs/html/here-docs.html – Peter Ajtai Jan 12 '15 at 18:05
  • 6
    Interesting note in PHP documentation comments: http://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 '17 at 09: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. – SherylHohman Jan 19 '19 at 20:45
  • +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}. I too agree with this. Just now got to know. – Avinash Dalvi Oct 21 '19 at 08:54
  • I write double quotes when it is needed. That way when I see a string starting with double quotes it's because we use features not possible with single quotes. I wish they did allow all esapes in single quotes except variable interpolation since that would have been perfect, but breaking change. – Sylwester Jun 29 '20 at 18:24
  • Would it make sense to follow the shell script convention? I think the $ sign is like an operator itself in php, which means "evaluate this variable as an expression". That's why instance variables are set by $this->name = "John", not this $this->$name = "John", because that would mean set something like $this->"Jimmy"="John" vs $this[0x0001] = "John". I think without the dollar sign the compiler converts the variable to its pointer – Jonathan Ma Sep 29 '20 at 01:16
  • @devios1 @aviboy2006 both `${variablename}` and `{$variablename}` in double quotes work! – C-Y Apr 19 '22 at 02:45
  • @SherylHohman that's actually a horrible note, for its conclusions and - expecially - for its bogus "performance rules". Use whatever quotes you like as it makes absolutely no difference is the only acceptable conclusion. – Your Common Sense Feb 13 '23 at 08:18
249

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.
Daniel
  • 30,896
  • 18
  • 85
  • 139
  • 1
    Escaped single quotes and escaped backslashes are expanded even in single quoted strings. – Peter Ajtai Aug 10 '10 at 05:34
  • 52
    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 '14 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. – Rostyslav Pylypenko Nov 20 '18 at 11:26
74

' Single quoted

The simplest way to specify a string is to enclose it in single quotes. Everything inside single quotes is treated as a plain string.

Example:

echo 'Start with a simple string';
echo 'Variables $var and escape sequences \n do not get interpolated';
echo 'You only need to escape the quote \' and a backslash itself\\';

" Double quoted

Strings in double quotes interpolate variables and a wide range of escape sequences. Note: Use curly braces {} to include complex variables or in case when next character after a variable is a valid character for a variable name.

Example:

echo "Start with a simple string";
echo "String's apostrophe";
echo "Escape sequences get interpolated\n";
echo "Variables $var get interpolated as well {$name}_sake";

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

No.

In order to improve performance, PHP parses the entire code once and then stores the resulting bytecode in the opcode cache. This approach eliminates the entire parsing, along with whatever quoited strings as well.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Janak Kanani
  • 880
  • 6
  • 6
  • 1
    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](http://php.net/manual/en/language.types.string.php) – SherylHohman Jan 19 '19 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 '19 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. – William Patton Aug 11 '21 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 Feb 08 '22 at 08:57
  • It also allows you to write `{$foo}bar`, as `$foobar` would get interpreted as `{$foobar}`. – alexia Oct 09 '22 at 12:17
43

A single-quoted string does not have variables within it interpreted. A double-quoted string does.

Also, a double-quoted string can contain apostrophes without backslashes, while a single-quoted string can contain unescaped quotation marks.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Borealid
  • 95,191
  • 9
  • 106
  • 122
  • 10
    Single quoted strings also use less memory. The fastest way to handle strings in PHP is with single quotes and using the . operator to concatenate strings and variables. – RibaldEddie Aug 10 '10 at 05:16
  • hmmm, correct me if I'm wrong but the base language for PHP is C right? Then why string quotes differ in PHP and C? – rob waminal Aug 10 '10 at 05:19
  • 2
    @rob waminal: PHP may be implemented in C, but it is a different language. The PHP language specifies these semantics. – Borealid Aug 10 '10 at 05:21
  • @Ribald - Wouldn't nowdoc syntax be faster? Single quoted strings are parsed for escaped single quotes and backslashes. – Peter Ajtai Aug 10 '10 at 05:33
  • @Peter, you may be correct, I've never bothered to really dig in to it. The PHP documentation makes the speed claim, I decided to believe the docs on faith. :) – RibaldEddie Aug 10 '10 at 05:57
  • 1
    @RibaldEddie now that's something new. Got any proof? – Your Common Sense Feb 13 '23 at 08:35
37

In PHP, both 'my name' and "my name" are string. You can read more about it at the PHP manual.

Thing you should know are

$a = 'name';
$b = "my $a"; == 'my name'
$c = 'my $a'; != 'my name'

In PHP, people use single quote to define a constant string, like 'a', 'my name', 'abc xyz', while using double quote to define a string contain identifier like "a $b $c $d".

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Bang Dao
  • 5,091
  • 1
  • 24
  • 33
13

Both kinds of enclosed characters are strings. One type of quote is conveniently used to enclose the other type of quote. "'" and '"'. The biggest difference between the types of quotes is that enclosed identifier references are substituted for inside double quotes, but not inside single quotes.

wallyk
  • 56,922
  • 16
  • 83
  • 148
4

Some might say that I'm a little off-topic, but here it is anyway:

You don't necessarily have to choose because of your string's content between:
echo "It's \"game\" time."; or echo 'It\'s "game" time.';

If you're familiar with the use of the english quotation marks, and the correct character for the apostrophe, you can use either double or single quotes, because it won't matter anymore:
echo "It’s “game” time."; and echo 'It’s “game” time.';

Of course you can also add variables if needed. Just don't forget that they get evaluated only when in double quotes!

Takit Isy
  • 9,688
  • 3
  • 23
  • 47