By rob waminal

2010-08-10 05:12:56 8 Comments

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.


@Janak Kanani 2014-04-12 03:54:24

' 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.


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.


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.

@SherylHohman 2019-01-19 20:48:53

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

@Rabindra Nath 2017-03-02 10:34:45

Example of single, double, heredoc, and nowdoc quotes


    $fname = "David";

    // Single quotes
    echo 'My name is $fname.'; // My name is $fname.

    // Double quotes
    echo "My name is $fname."; // My name is David.

    // Curly braces to isolate the name of the variable
    echo "My name is {$fname}."; // My name is David.

    // Example of heredoc
    echo $foo = <<<abc
    My name is {$fname}

        // Example of nowdoc
        echo <<< 'abc'
        My name is "$name".
        Now, I am printing some


@Takit Isy 2018-04-09 13:09:29

Maybe I'm a little late, and a little off-topic, but here it is anyway…

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

Instead, you can type like this, and then use either double or single quotes, because it won't matter:
alert("It’s “game” time."); and alert('It’s “game” time.');

@srmilon 2017-01-03 06:28:00

One thing:

It is very important to note that the line with the closing identifier of Heredoc must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon.


   $str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.

@miken32 2018-01-22 18:08:20

This is a true statement, but has nothing to do with the question here.

@Bang Dao 2010-08-10 05:23:10

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".

And other thing is,

echo 'my name';

is faster than

echo "my name";


echo 'my ' . $a;

is slower than

echo "my $a";

This is true for other used of string.

@Peter Ajtai 2010-08-10 05:28:33

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 what 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.

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.

@devios1 2013-07-25 00:26:56

+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}.

@Jo Smo 2014-07-04 13:26:09

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

@sjsam 2015-01-08 06:20:30

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

@Peter Ajtai 2015-01-12 18:05:06

@sjsam - looks like it - neat, didn't know that:

@Abeer Sul 2015-07-31 11:10:24

To remember: 'Single' for 'Strings' and "Double" for things in the "Database"..

@dregad 2017-08-10 09:45:31

Interesting note in PHP documentation comments: - "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."

@SherylHohman 2019-01-19 20:45:19

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.

@Fabien Haddadi 2019-04-12 02:09:38

Just spent 30' to carry on a batch of experiments with PHP 7 on the command line, calculating one million times the the length of 999 paragraphs of lorem ipsum (long, hoping it wouldn't be cached...), as a constant string right in the code, declared using 1) double-quotes, and 2) single quotes, then 3) double quotes and featuring a $ in front of a random word in it, with that variable defined above. Results: cases 1) and 2) : no significant difference between double quotes and single quotes, avg=17.0 microseconds, including changing random characters to avoid caching. But 24 s in case 3)!

@Maulik Bhojani 2014-02-06 09:19:23

In PHP, single quote text is considered as string value and double quote text will parse the variables by replacing and processing their value.

$test = "variable";
echo "Hello Mr $test"; // the output would be: Hello Mr variable
echo 'Hello Mr $test'; // the output would be: Hello Mr $test

Here, double quote parse the value and single quote is considered as string value (without parsing $test variable.)

@Dani 2010-08-10 05:19:34

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.

@Peter Ajtai 2010-08-10 05:34:26

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

@Kai Noack 2014-04-18 14:25:45

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.

@Rostyslav Pylypenko 2018-11-20 11:26:52

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.

@wallyk 2010-08-10 05:15:35

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.

@Borealid 2010-08-10 05:15:17

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.

The single-quoted strings are faster at runtime because they do not need to be parsed.

@RibaldEddie 2010-08-10 05:16:21

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.

@rob waminal 2010-08-10 05:19:36

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?

@Borealid 2010-08-10 05:21:33

@rob waminal: PHP may be implemented in C, but it is a different language. The PHP language specifies these semantics.

@Peter Ajtai 2010-08-10 05:33:35

@Ribald - Wouldn't nowdoc syntax be faster? Single quoted strings are parsed for escaped single quotes and backslashes.

@RibaldEddie 2010-08-10 05:57:02

@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. :)

Related Questions

Sponsored Content

64 Answered Questions

[SOLVED] What is the difference between String and string in C#?

39 Answered Questions

8 Answered Questions

[SOLVED] What does the 'b' character do in front of a string literal?

21 Answered Questions

[SOLVED] How to escape single quotes within single quoted strings

  • 2009-08-08 22:50:10
  • cons
  • 389761 View
  • 909 Score
  • 21 Answer
  • Tags:   bash syntax quoting

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

42 Answered Questions

[SOLVED] When to use double or single quotes in JavaScript?

7 Answered Questions

[SOLVED] How does PHP 'foreach' actually work?

6 Answered Questions

[SOLVED] Difference between single and double quotes in Bash

1 Answered Questions

[SOLVED] Do I need quotes for strings in YAML?

  • 2013-10-01 07:00:47
  • Alex Popov
  • 135642 View
  • 336 Score
  • 1 Answer
  • Tags:   syntax yaml quotes

Sponsored Content