Home > Article > Backend Development > The difference between single quotes and double quotes in PHP_PHP Tutorial
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>
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>
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>
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>
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.