In PHP, the definition of a string can use English single quotes ' '
, you can also use English double quotes " "
.
But the same type of single or double quotes must be used to define the string, such as: 'Hello World"
and "Hello World'
are illegal string definitions.
What is the difference between single quotes and double quotes?
PHP allows us to include 字串变量
directly in a double quote string.
The content in single quotation marks is always considered as ordinary characters, so the content in single quotation marks will not be escaped, which is more efficient.
For example:
<span>$str='hello'; echo "str is $str"; //运行结果: str is hello echo 'str is $str'; //运行结果: str is $str</span><br /><br /><span>php中,双引号中的变量($var)和特殊字符(\r\n之类)会被转义,单引号中的内容不会被转义(所以效率更高)。</span><br /><span>使用上的话,</span><br /><span>我以前很喜欢在sql字符串里这样写$sql = "SELECT * FROM table WHERE id = $id",这样里面的$id可以被转义,单引号就不行。</span><br /><br /><span>在JavaScript中 单引号 和双引号没有区别,只要成对使用就行。<br />我在JavaScript 中使用单引号大多是因为 Javascript 和 HTML 打交道比较多,输出 HTML 片段的时候不需要转义 HTML 中属性的引号。<br />总之,看实际情况来用啦,怎么方便怎么使用。<br /></span><br /><br /><br /><br /><br />