Home > Backend Development > PHP Tutorial > The difference between single quotes and double quotes in PHP_PHP Tutorial

The difference between single quotes and double quotes in PHP_PHP Tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-20 10:56:54
Original
1067 people have browsed it

In programming languages, whether it is single quotes or double quotes, they all play a very important role, and the same is true in the PHP language . Compared with ASP, PHP's quotation marks are easier to use. In ASP, if you want to substitute data into a variable, you must use double quotation marks, and if quotation marks are used inside, you can only use single quotation marks, not double quotation marks. If If double quotes are used, they will be treated as the end of the previous quote.

But there is no such restriction in PHP. To substitute values ​​into variables, both single quotes and double quotes can be used, but they must be used in pairs.

In PHP, if it is only used for text data that does not contain variables, there is no difference between single quotes and double quotes. But if you want to use variables, there is a difference between single quotes and double quotes.

In PHP, variables can be directly substituted in double quotes without converting definitions or other symbols,

such as:

<ol class="dp-c"><li class="alt"><span><span class="vars">$b</span><span> = “cat”;</span><span class="vars">$a</span><span> = “This is a </span><span class="vars">$b</span><span>”;</span><span class="comment">//显示This is a cat</span><span> </span></span></li></ol>
Copy after login

single Quotes will not work. If

<ol class="dp-c"><li class="alt"><span><span class="vars">$a</span><span> = ‘This is a </span><span class="vars">$b</span><span>’;</span><span class="comment">//则显示为:This is a $b。</span><span> </span></span></li></ol>
Copy after login

there is also a difference between single quotes and double quotes in terms of operating efficiency. Generally speaking, single quotes will run faster and double quotes will run faster. The reason is that double quotation marks must first search whether there are variables in the statement, but single quotation marks are not used. Therefore, if there is no variable substituted in the statement, try to use single quotation marks. This is a habit of writing programs, always thinking about improving the efficiency of the program.

If you want to convert the definition operation in the statement, you must use double quotes.

For example, when converting the definition of single quotes, write it like this:

<ol class="dp-c"><li class="alt"><span><span class="vars">$a</span><span> = ‘He’s name is Tom.’ ; </span></span></li></ol>
Copy after login

The program will display He's name is Tom. intact. If so Write:

<ol class="dp-c"><li class="alt"><span><span class="vars">$a</span><span> = “He’s name is Tom.”; </span></span></li></ol>
Copy after login

The program will display: He's name is Tom.

Single quotation marks are a stumbling block for sql statements. Text data in sql statements are all It must be included in single quotes, so if single quotes appear in the data, the database will consider the data to be over, and then the subsequent data will be considered to be other components of the SQL statement. Of course, an error will be reported during the database query, so write the SQL statement The text data must be converted to single quotes using the addslashes() function, and then converted back using stripslashes() when reading the data.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445808.htmlTechArticleIn programming languages, both single quotes and double quotes play a very important role. In PHP language Same thing. Compared with ASP, PHP's quotation marks are easier to use. In ASP, you need to represent data...
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 Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template