The difference between single quotes and double quotes in php

伊谢尔伦
Release: 2023-03-02 17:24:01
Original
2718 people have browsed it

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 include string variables directly 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:

$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 (rn and the like) in double quotes will be escaped, but the content in single quotes will not be escaped (so it is more efficient).

In terms of use, I used to like to write $sql = "SELECT * FROM table WHERE id = $id" in the sql string, so that the $id inside can be escaped, but single quotes cannot.

There is no difference between single quotes and double quotes in JavaScript, as long as they are used in pairs.

I mostly use single quotes in JavaScript because Javascript deals 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, how convenient and how to use it.


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