30

Possible Duplicate:
PHP: different quotes?

Simple question:

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

Community
  • 1
  • 1
johnnietheblack
  • 13,050
  • 28
  • 95
  • 133
  • 7
    Dupe: http://stackoverflow.com/questions/1318028/php-different-quotes – strager Sep 09 '09 at 22:55
  • Please search stackoverflow (the searchbox is in the top right corner) before posting a new question. – ChristopheD Sep 09 '09 at 22:57
  • i did, actually. apparently just not well enough. thanks – johnnietheblack Sep 09 '09 at 22:58
  • 2
    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 09 '09 at 23:04
  • @Johannes Rössel, Google helps with searching for me at times. =] – strager Sep 09 '09 at 23:09

6 Answers6

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.

Josh Davis
  • 28,400
  • 5
  • 52
  • 67
23

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

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.
Abhishek Bhatia
  • 716
  • 9
  • 26
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
5

Any variables inside a " quoted string will be parsed. Any variables in a ' quoted string will not be parsed, and will be shown literally as the variable name. For this reason, ' quoted strings are very slightly faster for PHP to process.

$test = 'hello';
echo "this is a $test"; // returns this is a hello
echo 'this is a $test'; // returns this is a $test

I'd say use ' quotes unless you want variables inside your strings.

1

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.

Brenton Alker
  • 8,947
  • 3
  • 36
  • 37
0

" interprets escape characters and variables. ' doesn't do either.

igustin
  • 1,110
  • 8
  • 8
0

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

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66