Home  >  Article  >  Backend Development  >  The difference between single quotes and double quotes in php strings

The difference between single quotes and double quotes in php strings

藏色散人
藏色散人Original
2019-08-23 13:54:266449browse

The difference between single quotes and double quotes in php strings

The difference between single quotes and double quotes in php strings

In PHP, the definition of strings can be used English single quotation marks ' ', English double quotation marks " " can also be used.

Generally, the two are common, but the double quotes internal variables will be parsed, but the single quotes will not be parsed.

PHP allows us to include string variables directly in double-quoted strings.

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.

Recommended: [PHP Tutorial]

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) will be escaped, and the content in single quotes will not be escaped (so it is 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 the $id inside can be transferred righteousness, single quotation marks will not work.

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.

The above is the detailed content of The difference between single quotes and double quotes in php strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn