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

The difference between single quotes and double quotes in PHP

WBOY
Release: 2016-08-08 09:25:17
Original
928 people have browsed it

Reprinted from: http://developer.51cto.com/art/201105/263107.htm

In programming languages, whether it is single quotes or double quotes, they all play a very important role in PHP The same is true in 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 limitation 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.

For example:

  1. $b = “cat”;$a = “This is a $b”;//显示This is a cat 

Single quotes will not work. If

  1. $a = ‘This is a $b’;//则显示为:This is a $b。 

there is also a difference between single quotes and double quotes in terms of operating efficiency Generally speaking, single quotation marks will run faster and double quotation marks will run slower. The reason is that double quotation marks need to first search whether there are variables in the statement, while single quotation marks are not used. Therefore, if there is no variable substituted in the statement, try to use single quotation marks. 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 redefining single quotes, if you write it like this:

  1. $a = ‘He\’s name is Tom.’ ; 

The program will display He's name is Tom.

Single quotes are a stumbling block for sql statements. Textual data in sql statements must be enclosed in single quotes. Therefore, if single quotes appear in the data, the database will consider the end of the data, and then the subsequent data will be considered a sql statement. Of course, other components will report an error when querying the database, so the text data written into the SQL statement must be converted to single quotes using the addslashes() function, and then converted back using stripslashes() when reading the data.

The above introduces the difference between single quotation marks and double quotation marks in PHP, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.


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