views:

511

answers:

7

Hi, I'm not a major in PHP programming but I'm a little confused why I see some codes in PHP with string placed in single quote and sometimes in double quote. I just know in .NET, or C language, if it is in single quote, that means it is a character, not a string.

+8  A: 

A single-quoted string does not have variables within it interpreted. A double-quoted string does.

Also, a double-quoted string can contain apostrophes without backslashes, while a single-quoted string can contain unescaped quotation marks.

The single-quoted strings are faster at runtime because they do not need to be parsed.

Borealid
Single quoted strings also use less memory. The fastest way to handle strings in PHP is with single quotes and using the . operator to concatenate strings and variables.
RibaldEddie
hmmm, correct me if I'm wrong but the base language for PHP is C right? Then why string quotes differ in PHP and C?
rob waminal
@rob waminal: PHP may be implemented in C, but it is a different language. The PHP language specifies these semantics.
Borealid
@Ribald - Wouldn't nowdoc syntax be faster? Single quoted strings are parsed for escaped single quotes and backslashes.
Peter Ajtai
@Peter, you may be correct, I've never bothered to really dig in to it. The PHP documentation makes the speed claim, I decided to believe the docs on faith. :)
RibaldEddie
+2  A: 

Both kinds of enclosed characters are strings. One type of quote is conveniently used to enclose the other type of quote. "'" and '"'. The biggest difference between the types of quotes is that enclosed identifier references are substituted for inside double quotes, but not inside single quotes.

wallyk
+3  A: 

Things get evaluated in double quotes but not in single:

$s = "dollars";
echo 'This costs a lot of $s.'; // This costs a lot of $s.
echo "This costs a lot of $s."; // This costs a lot of dollars.
Dani
Escaped single quotes and escaped backslashes are expanded even in single quoted strings.
Peter Ajtai
+1  A: 

In PHP, both 'my name' and "my name" are string. You can read more about it at http://www.php.net/manual/en/book.strings.php
Thing you should know are
$a = 'name';
$b = "my $a"; == 'my name'
$c = 'my $a'; != 'my name'
In php, people use single quote to define a constant string, like 'a', 'my name', 'abc xyz', while using double quote to define a string contain identifier like "a $b $c $d".
And other thing is,

echo 'my name';

is faster than

echo "my name";

but

echo 'my ' . $a;

is slower than

echo "my $a";

This is true for other used of string.

Bang Dao
+9  A: 

PHP strings can be specified not just in two ways, but in four ways.

  1. Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\ (So yes, even single quoted strings are parsed).
  2. Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable $type and you what to echo "The $types are" That will look for the variable $types. To get around this use echo "The {$type}s are" You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such.
  3. Heredoc string syntax works like double quoted strings. It starts with <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax.
  4. Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.

Speed:
I would not put too much weight on single quotes being faster than double quotes. They probably are faster in certain situations. Here's an article explaining one manner in which single and double quotes are essentially equally fast since PHP 4.3 (Useless Optimizations toward the bottom, section C). Also, this benchmarks page has a single vs double quote comparison. Most of the comparisons are the same. There is one comparison where double quotes are slower than single quotes.

Peter Ajtai
hmm nice one, I learn new thing here. Actually this is my first time to hear Heredoc, and Nowdoc. I will read this two.
rob waminal
+1  A: 

The "Quote Types" section of http://www.phpbench.com/ provides a useful quantitative explanation of single and double quotes.

Zach Rattner
A: 

In PHP, double quotes string is faster and confirmed by by the core developer:

http://www.linuxask.com/questions/should-i-always-use-single-quotes-for-php-strings

Howard