Possible Duplicate:
Difference between single quote and double quote string in php
I'm a newbie to PHP and I'm getting a little confused about how to specify strings.
There seem to be 4 ways to specify strings
I was wondering why there are so many types of specifications and what is the best way to specify a string?
Because again in this article it says single and double quotes are equally fast but there are others which disagree.
Why even have single and double quotes? Does it add speed, accuracy is any one better under certain conditions?
Single-quoted strings pay attention to less escape sequences than double-quoted strings. Compare:
echo "one
two";
# one
# two
with:
echo 'one
two';
# one
two
The other key difference is that double-quoted strings interpolate variables. Single-quoted strings don't.
As for heredoc and nowdoc, they're just ways to specify long strings (that may contain quotes - and if they do, you don't have to escape them) in a bit of a nicer way. Why are there two? Well, heredoc is to double-quoted strings as nowdoc is to single-quoted strings.
(All this information and more is available in the PHP documentation, by the way.)
So, there are a lot of different types of string literal because there are a lot of different uses for string literals.
http://php.net/manual/en/language.types.string.php
In terms of performance, single-quoted strings are usually preferable to double-quoted strings, as they basically represent constants. So for the most part you will want to use single-quoted strings.
However, often enough, double-quoted strings can be very practical when preparing output
$combined = "An $a and $b plus $c";
$combined = 'An '.$a.' and '.$b.' plus '.$c;
Those lines are equal, but you can probably guess which is easier to write and maintain.
You should maybe keep in mind that while this is fairly specific to PHP, most other programming languages support similar syntax but handle them equally. So for those they really only exist to make writing quotes inside strings easier (Consider "Someone can't handle an apostrophe, but it's not me."
or '<p class="some">Text.</p>'
) by not having to escape each one.
Every character and variable within single quotation marks is treated literally (with one exception being the combination of \', see "Escaping Characters" below). The following code will not have the intended result of printing the variable's value:
$name = 'Jon';
echo 'Hello, $name';
That code will literally print "Hello, $name".
Conversely, characters and variables within double quotation marks will be interpreted. This code will print "Hello, Jon":
$name = 'Jon';
echo "Hello, $name";
Almost all of the special escape sequences also need to be used within double quotation marks. Because of the interpretation that occurs with double-quoted strings, they are arguably less efficient than single-quoted strings. For most applications, the efficiency difference is irrelevant, though.