Here some possibilities of single and double quotes with variable
$world = "world";
"Hello '.$world.' ";
'hello ".$world."';
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.
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.
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.
In PHP, single quote text is considered as string value and double quote text will parse the variables by replacing and processing their value.
$test = "variable";
echo "Hello Mr $test"; // the output would be: Hello Mr variable
echo 'Hello Mr $test'; // the output would be: Hello Mr $test
Here, double quote parse the value and single quote is considered as string value (without parsing the $test
variable.)
The difference between using single quotes and double quotes in php is that if we use single quotes in echo statement then it is treated as a string. ... but if we enter variable name within double quotes then it will output the value of that variable along with the string.
One thing:
It is very important to note that the line with the closing identifier of Heredoc must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon.
Example:
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
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"
.
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.
<?php
$fname = "David";
// Single quotes
echo 'My name is $fname.'; // My name is $fname.
// Double quotes
echo "My name is $fname."; // My name is David.
// Curly braces to isolate the name of the variable
echo "My name is {$fname}."; // My name is David.
// Example of heredoc
echo $foo = <<<abc
My name is {$fname}
abc;
// Example of nowdoc
echo <<< 'abc'
My name is "$name".
Now, I am printing some
abc;
?>
'
Single quotedThe simplest way to specify a string is to enclose it in single quotes. Single quote is generally faster, and everything quoted inside treated as plain string.
Example:
echo 'Start with a simple string';
echo 'String\'s apostrophe';
echo 'String with a php variable'.$name;
"
Double quotedUse double quotes in PHP to avoid having to use the period to separate code (Note: Use curly braces {}
to include variables if you do not want to use concatenation (.
) operator) in string.
Example:
echo "Start with a simple string";
echo "String's apostrophe";
echo "String with a php variable {$name}";
Yes. It is slightly faster to use single quotes.
PHP won't use additional processing to interpret what is inside the single quote. when you use double quotes PHP has to parse to check if there are any variables within the string.
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!
Source: Stackoverflow.com