1

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

  1. Single quoted strings
  2. Double quote strings
  3. Heredoc
  4. Nowdoc (since PHP 5.3.0)

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?

Community
  • 1
  • 1
cjds
  • 8,268
  • 10
  • 49
  • 84
  • double quote will also check for variables, so I guess it bit slower – Jonathan de M. Dec 19 '12 at 03:17
  • 1
    The actual reason for so many ways of creating strings is mostly historical. PHP has taken inspiration from various shell scripting languages, as well as other programming languages such as C, Perl, AWK, etc. – Sverri M. Olsen Dec 19 '12 at 03:48

3 Answers3

2

Single-quoted strings pay attention to less escape sequences than double-quoted strings. Compare:

echo "one\ntwo";
# one
# two

with:

echo 'one\ntwo';
# one\ntwo

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.

Ry-
  • 218,210
  • 55
  • 464
  • 476
2

http://php.net/manual/en/language.types.string.php

  • Single-quoted strings are basically normal strings. They are only parsed when the script is compiled
  • Double-quoted strings are parsed at runtime
  • Heredocs are a way to define double-quoted strings over multiple lines using a special sequence for the beginning and end
  • Nowdocs are what Heredocs are, just unparsed like single-quoted strings

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.

dualed
  • 10,262
  • 1
  • 26
  • 29
0

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.

rOcKiNg RhO
  • 631
  • 1
  • 6
  • 16