开发者

php syntax question [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Difference between single quote and double quote string in php

What's the difference between single quote ' and double quote " in php? My idea is when one uses a single quote, there is some core value in it that needs to be used in the program, in case of double quote its just a string. Am I cor开发者_开发技巧rect?


The biggest difference is that if you use double quotes, any variables or special escape codes inside of the string will be expanded; whereas with single quotes, no expansion will occur.

Example:

$var = "foobar";
echo $var; // "foobar"
echo '$var'; // "$var"
echo "$var"; // "foobar"

echo '\n'; // "\n"
echo "\n"; // prints an actual new line

See the PHP documentation on strings for the full list of differences: http://us3.php.net/manual/en/language.types.string.php


Strings created with double quotes will be parsed by PHP, strings in single quotes won't. There are other differences too, such as for strings in double quotes PHP will interpret escape sequences for special characters. The PHP manual explains the differences in full.

For example,

$string = 'World';
echo 'Hello' . $string; // "Hello World"
echo "Hello $string"; // "Hello World"
echo 'Hello $string'; // "Hello $string"


In addition to variable expansion already described, double quotes also interpret \n (as newline), \t (as tab) and other escape sequences defined in the manual. The single quotes only interpret \\ as one backslash and \' as single quote, other combinations are understood literally - i.e. strlen('\n') == 2, strlen("\n") == 1


the main difference between these two is strings created with double quotes will be parsed by PHP and single quotes wo'nt.

// they both output 'Yahoo Google'
$str = 'Google';
echo 'Yahoo' . $str;
echo "Yahoo $str";

for more about these See below link

http://ca.php.net/types.string


Apart from what is mentioned in other answers, if you use single quotes ', double quotes inside will be treated as a normal character and need not be escaped and vice-versa.

For example both will print He said, "Hi there Jack"

echo 'He said, "Hi there Jack"';  

echo "He said, \"Hi there Jack\"";
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜