Reputation: 18419
I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes.
I just know in .NET, or the C language, if it is in a single quote, that means it is a character, not a string.
Upvotes: 941
Views: 487860
Reputation: 57685
PHP strings can be specified not just in two ways, but in four ways.
\'
, and to display a back slash, you can escape it with another backslash \\
(So yes, even single quoted strings are parsed).$type
and you want to echo "The $types are"
. That will look for the variable $types
. To get around this use echo "The {$type}s are"
. Take a look at string parsing to see how to use array variables and such.<<<
. 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.<<<
sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'
. No parsing is done in nowdoc.Notes: Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:
$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";
Speed:
There is no difference.
Please read a credible article on the matter from one of PHP core developers. Speaking of tests, one should never take them for granted. It must be understood that writing a credible test and, especially, interpreting its results requires a lot of knowledge and experience. Which means that most tests out there are just bogus. For example, in a code like this
for($i=0;$i<100000;$i++) {
'string';
}
The quoted string gets parsed only once, along with entire script, and then gets translated into opcode. Which is then gets executed a million times. So it measures anything but parsing. And that's only a tip of the iceberg. For a nanobenchmark like this, it's practically impossible to create a credible test that wouldn't be spoiled by some interfering side effect.
Upvotes: 1246
Reputation: 5102
In PHP, both 'my name'
and "my name"
are string. You can read more about it at the PHP manual.
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"
.
Upvotes: 37
Reputation: 98459
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.
Upvotes: 44
Reputation: 890
'
Single quotedThe simplest way to specify a string is to enclose it in single quotes. Everything inside single quotes is treated as a plain string.
Example:
echo 'Start with a simple string';
echo 'Variables $var and escape sequences \n do not get interpolated';
echo 'You only need to escape the quote \' and a backslash itself\\';
"
Double quotedStrings in double quotes interpolate variables and a wide range of escape sequences. Note: Use curly braces {}
to include complex variables or in case when next character after a variable is a valid character for a variable name.
Example:
echo "Start with a simple string";
echo "String's apostrophe";
echo "Escape sequences get interpolated\n";
echo "Variables $var get interpolated as well {$name}_sake";
No.
In order to improve performance, PHP parses the entire code once and then stores the resulting bytecode in the opcode cache. This approach eliminates the entire parsing, along with whatever quoited strings as well.
Upvotes: 75
Reputation: 10071
Some might say that I'm a little off-topic, but here it is anyway:
You don't necessarily have to choose because of your string's content between:
echo "It's \"game\" time.";
or echo 'It\'s "game" time.';
If you're familiar with the use of the english quotation marks, and the correct character for the apostrophe, you can use either double or single quotes, because it won't matter anymore:
echo "It’s “game” time.";
and echo 'It’s “game” time.';
Of course you can also add variables if needed. Just don't forget that they get evaluated only when in double quotes!
Upvotes: 4
Reputation: 31559
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.
Upvotes: 254
Reputation: 57764
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.
Upvotes: 13