开发者

What difference does it makes when I use '' against ""?

What difference does it makes when I use '' against "" ?

For example:

$example = '开发者_运维问答Merry Christmas in Advance';
$eg = "Merry Christmas";

echo "$example";
echo '$example';


echo "$eg";
echo '$eg';

What would the output for each echo statements and what can we infer about '' vs "" in PHP ?


$example = 'Merry Christmas in Advance';
$eg = "Merry Christmas";

echo "$example";
echo '$example';

echo "$eg";
echo '$eg';

would produce:

Merry Christmas in Advance$exampleMerry Christmas$eg

Single quoted strings are handled literally. No special characters (such as \n) or variables are interpolated.

Double quoted strings will interpolate your variables and special characters and render them accordingly.


Single-quoted variables take input literally, double-quotes interpret escape sequences for special chars and expand variables.

You can see some good examples here: http://php.net/manual/en/language.types.string.php

Note that SOME escape sequences are still interpreted within single quotes. Example:

// Outputs: Arnold once said: "I'll be back"

echo 'Arnold once said: "I\'ll be back"';


You can also include variables inside double quotes and those will be interpreted as variables, rather than strings

So:

$variable = 1;
echo 'this $variable' ==> will output 'this $variable'
echo "this $variable" ==> will output 'this 1'


Double quotes interpolate variables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜