What are the differences between single quotes and double quotes in php

coldplay.xixi
Release: 2023-03-08 10:18:01
Original
4962 people have browsed it

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.

What are the differences between single quotes and double quotes in php

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

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!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!