Home >Backend Development >PHP Problem >What are the differences between single quotes and double quotes in php
The difference between single quotes and double quotes in php: 1. Variables inside double quotes will be parsed, but single quotes will not be parsed; 2. Variables [$var] and special characters [\r\] in double quotes n and so on] will be escaped, but the content in single quotes will not be escaped.
The operating environment of this tutorial: Windows 7 system, PHP version 5.6, DELL G3 computer. This method is suitable for all brands of computers.
The difference between single quotes and double quotes in PHP:
In PHP, the definition of a string can use English single quotes ' ' or English double quotes Quotation marks " ".
Under normal circumstances, the two are common. However, double quotes internal variables will be parsed, but single quotes will not be parsed.
PHP allows us to use double quotes in strings Contains string variables directly.
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:
$str='hello'; echo "str is $str"; //运行结果: str is hello echo 'str is $str'; //运行结果: str is $str
In php, variables ($var) and special characters (\r\n and the like) in double quotes will be escaped. Content within quotes is not escaped (so more efficient).
In terms of use,
I used to like to write like this in the sql string $sql = "SELECT * FROM table WHERE id = $id"
, so that $id inside can be escaped, but single quotes cannot.
In JavaScript, there is no difference between single quotes and double quotes, as long as they are used in pairs.
I mostly use single quotes in JavaScript because Javascript interacts with HTML a lot. When outputting HTML fragments, there is no need to escape the quotes of attributes in HTML.
In short, it depends on the actual situation and how to use it conveniently.
Related free learning recommendations: php programming (video)
The above is the detailed content of What are the differences between single quotes and double quotes in php. For more information, please follow other related articles on the PHP Chinese website!