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

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.

Solution #1:

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.

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?"";

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.

Respondent: Peter Ajtai

Solution #2:

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.
Respondent: Dani

Solution #3:

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

Respondent: Janak Kanani

Solution #4:

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.

Respondent: Borealid

Solution #5:

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.

Respondent: Bang Dao

Solution #6:

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

Respondent: Rabindra Nath

Solution #7:

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 the $test variable.)

Respondent: Maulik Bhojani

Solution #8:

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.

Respondent: wallyk

Solution #9:

The difference between using single quotes and double quotes in php is that if we use single quotes in echo statement then it is treated as a string. … but if we enter variable name within double quotes then it will output the value of that variable along with the string.

Respondent: Hamza

Solution #10:

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!

Respondent: Takit Isy

Solution #11:

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.
Respondent: srmilon

Solution #12:

Here some possibilities of single and double quotes with variable

$world = "world";

"Hello '.$world.' ";

'hello ".$world."';
Respondent: teenage vampire

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy