syntax - What is the difference between ' and " in PHP?
[Link]/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: [Link]/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