0% found this document useful (0 votes)
8 views4 pages

What is the difference between ' and _ in PHP_ - Stack Overflow

In PHP, single-quoted strings (' ') treat the text as plain text without variable interpolation or escape sequences, while double-quoted strings (' ') allow for variable expansion and special character interpretation. For example, using double quotes enables the output of variable values within the string, whereas single quotes will display the variable name literally. Additionally, heredoc syntax behaves like double-quoted strings but without the need for enclosing quotes.

Uploaded by

agcolab92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

What is the difference between ' and _ in PHP_ - Stack Overflow

In PHP, single-quoted strings (' ') treat the text as plain text without variable interpolation or escape sequences, while double-quoted strings (' ') allow for variable expansion and special character interpretation. For example, using double quotes enables the output of variable values within the string, whereas single quotes will display the variable name literally. Additionally, heredoc syntax behaves like double-quoted strings but without the need for enclosing quotes.

Uploaded by

agcolab92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

syntax - What is the difference between ' and " in PHP?

stackoverflow.com/questions/1402566/what-is-the-difference-between-and-in-php

johnnietheblack 13.1k2828 gold badges9797 silver badges133133 bronze badges

What is the difference between ' and " in PHP? [duplicate]


Ask Question
Asked 14 years, 4 months ago

Modified 11 years, 4 months ago


Viewed 58k times

Part of PHP Collective

AI feature from

Start a conversation with our new experimental AI


Explore programming topics and find answers in a private space.

Try it now
30

This question already has answers here:

Closed 14 years ago.

Possible Duplicate:
PHP: different quotes?
Simple question:

What is the difference between ' and " in php? When should I use either?
phpsyntax

Dupe: stackoverflow.com/questions/1318028/php-different-quotes – strager Sep 9,


2009 at 22:55

The larger a repository of stuff gets the harder it is to actually find something. The
absolute worst example I've seen so far is Mozilla's bug tracker. Stack Overflow
gets close with some topics where you can't really be sure of the terms used and
even less sure of the search terms to use to find it. – Joey Sep 9, 2009 at 23:04

Add a comment

6 Answers

25

Basically, single-quoted strings are plain text with virtually no special case whereas
double-quoted strings have variable interpolation (e.g. echo "Hello $username";) as
well as escaped sequences such as "\n" (newline.)

You can learn more about strings in PHP's manual.

answered Sep 9, 2009 at 22:57

Josh Davis

23

There are 3 syntax used to declare strings, in PHP <= 5.2 :

single quoted
double quoted
heredoc

With single quotes :

variables and escape sequences for special characters will not be expanded
For instance :

echo 'Variables do not $expand $either';

Will output :

Variables do not $expand $either

With double-quotes :

The most important feature of double-quoted strings is the fact that variable names
will be expanded.

For instance :

$a = 10;
echo "a is $a";

Will output :

a is 10

And, with heredoc :

Heredoc text behaves just like a double-quoted string, without the double quotes.
This means that quotes in a heredoc do not need to be escaped,

For instance :

$a = 10;
$b = 'hello';

$str = <<<END_STR
a is $a
and "b" is $b.
END_STR;

echo $str;

Will get you :

a is 10
and "b" is hello.
edited Aug 31, 2012 at 11:45

Abhishek Bhatia
71699 silver badges2626 bronze badges

answered Sep 9, 2009 at 22:57


Pascal MARTIN
397k80659 silver badges664664 bronze badges
5

The difference is, strings between double quotes (") are parsed for variable and escape
sequence substitution. Strings in single quotes (') aren't.

So, using double quotes (") you can do:

$count = 3;
echo "The count is:\t$count";

which will produce

The count is:<tab>3

The same in single quotes returns the literal string.

Also, the characters that need to be escaped. If you have a string like:

'John said, "Hello"'

you would probably use single quotes, to avoid having to escape the quotes in the string
and vice-versa.

answered Sep 9, 2009 at 22:57

Brenton Alker
8,97233 gold badges3636 silver badges3737 bronze badges

Add a comment
0

In one word: when you would like to all your special chars (like \n) and varables (like
$number) be noticed and process.

answered Sep 9, 2009 at 22:57

IProblemFactory
9,66988 gold badges5151 silver badges6666 bronze badges

Add a comment

You might also like