Home >Backend Development >PHP Tutorial >The difference between single and double quotes in php
In PHP, the definition of a string can use English single quotes ' '
, or 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 directly include string variables
in double quoted strings.
The content in single quote strings is always considered as ordinary characters, so the content in single quotes 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><span>php中,双引号中的变量($var)和特殊字符(\r\n之类)会被转义,单引号中的内容不会被转义(所以效率更高)。</span><br><span>使用上的话,</span><br><span>我以前很喜欢在sql字符串里这样写$sql = "SELECT * FROM table WHERE id = $id",这样里面的$id可以被转义,单引号就不行。</span><br><span>在JavaScript中 单引号 和双引号没有区别,只要成对使用就行。<br>我在JavaScript 中使用单引号大多是因为 Javascript 和 HTML 打交道比较多,输出 HTML 片段的时候不需要转义 HTML 中属性的引号。<br>总之,看实际情况来用啦,怎么方便怎么使用。<br></span><br>
The above introduces the difference between single and double quotation marks in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.