The difference between single quotes and double quotes in php strings

藏色散人
Release: 2023-02-23 10:36:02
Original
6452 people have browsed it

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template