Home > Backend Development > PHP Tutorial > Discuss the difference between single and double quotation marks in PHP programming language_PHP Tutorial

Discuss the difference between single and double quotation marks in PHP programming language_PHP Tutorial

WBOY
Release: 2016-07-20 10:56:53
Original
1140 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. Let's introduce the difference between single quotation marks and double quotation marks.

1. Quotes define strings.

To achieve the purpose of including quotation marks, the parser must ignore its original meaning when encountering ordinary quotation marks in the string. We add a backslash in front of the quotation mark to tell PHP: This quotation mark It is part of the string. The correct expression method is this: single quote string can be used in more related places. Using single quote string in script (batch file) will process faster and faster. It should be PHP syntax parser. The processing method for single-quote strings is relatively simple, while the processing of double-quotes is more complicated because the string also needs to be parsed internally, so the processing speed is slightly slower.

The double quotes are escaped, but the single quotes are not escaped. In PHP, a string is usually defined in a pair of quotation marks, such as:

<ol class="dp-c">
<li class="alt"><span><span class="string">'I am a string in single quotes'</span><span>   </span></span></li>
<li>
<span class="string">"I am a string in double quotes"</span><span>  </span>
</li>
</ol>
Copy after login

The PHP syntax parser uses pairs of quotation marks to judge a string. Therefore, all strings must use the same single or double quotes to define the beginning and end. For example, the following string definition is illegal:

<ol class="dp-c">
<li class="alt"><span><span>"I am not a valid string since I have unmatching quote marks'   </span></span></li>
<li><span>'Me neither!"  </span></li>
</ol>
Copy after login

When defining a string, only one type of quotation mark is considered a delimiter, that is, a single quotation mark Quotation marks or double quotes. Thus, if a string begins with a double quote, only the double quote is parsed by the parser. This way, you can include any more relevant characters in the double-quoted string, even single quotes. The following quotation mark strings are all legal:

<ol class="dp-c">
<li class="alt"><span><span class="vars">$s</span><span> = </span><span class="string">"I am a 'single quote string' inside a double quote string"</span><span>;   </span></span></li>
<li>
<span class="vars">$s</span><span> = </span><span class="string">'I am a "double quote string" inside a single quote string'</span><span>;  </span>
</li>
</ol>
Copy after login

When PHP encounters a quotation mark corresponding to the beginning of the string, it considers that the string has been reached Tail, so:

<ol class="dp-c"><li class="alt"><span><span class="string">"Why doesn't "</span><span>this</span><span class="string">" work?"</span><span>  </span></span></li></ol>
Copy after login

is actually divided into three parts by the PHP syntax parser:

"Why doesn't "— — A double-quoted string containing a single quote

this — extra characters that the parser cannot handle

" work?" — a normal string

above This CASE attempts to include double quotes within a double quote string, and the parser considers the string to end when it encounters the second double quote. To achieve the purpose of including quotation marks, the parser must ignore its original meaning when encountering ordinary quotation marks in the string. We add a backslash in front of the quotation mark to tell PHP: this quotation mark is part of the string, the correct expression The method is like this:

<ol class="dp-c"><li class="alt"><span><span class="string">"Why doesn't "that" work?"</span><span>  </span></span></li></ol>
Copy after login

A common problem in English strings is the use of apostrophe ', which should be a single quote at all, and Very common in English strings (English possessive case). You have to be careful with these characters:

<ol class="dp-c"><li class="alt"><span><span class="string">'You'd better escape your apostrophes'</span><span>  </span></span></li></ol>
Copy after login

You can see that backslash has its special meaning in strings, when we need to When a backslash itself is included, an additional backslash is required before the symbol. For example:

<ol class="dp-c">
<li class="alt"><span><span class="vars">$file</span><span> = </span><span class="string">"c:windowssystem.ini"</span><span>;   </span></span></li>
<li>
<span class="func">echo</span><span> </span><span class="vars">$file</span><span>; </span><span class="comment">// 打印结果为: c:windowssystem.ini  </span><span> </span>
</li>
<li class="alt">
<span class="vars">$file</span><span> = </span><span class="string">"c:\windows\system.ini"</span><span>;   </span>
</li>
<li>
<span class="func">echo</span><span> </span><span class="vars">$file</span><span>; </span><span class="comment">// 打印结果为: c:windowssystem.ini </span><span> </span>
</li>
</ol>
Copy after login

Another string definition method that eliminates the trouble of special characters and makes it easier to quote longer texts. This string definition method starts with the <<< symbol followed by a custom string, and the last line ends with the custom string and must be in a box.

2. String links

Strings can be linked using the string linker (.), such as:

<ol class="dp-c"><li class="alt"><span><span class="vars">$first_name</span><span> = </span><span class="string">'Charlie'</span><span>;   </span></span></li><li><span class="vars">$last_name</span><span> = </span><span class="string">'Brown'</span><span>;   </span></li><li class="alt"><span class="vars">$full_name</span><span> = </span><span class="vars">$first_name</span><span> . </span><span class="string">' '</span><span> . </span><span class="vars">$last_name</span><span>;  </span></li></ol>
Copy after login

A common use is to create large blocks of HTML string source code. The assignment symbol (=) and link symbol (.) can be abbreviated and merged into (.=) symbols, such as:

<ol class="dp-c"><li class="alt"><span><span class="vars">$html</span><span> = </span><span class="string">'<table>'<span>;   </span></p>
<li>
<span class="vars">$html</span><span> .= </span><span class="string">'<tr><td>number</td><td>square</td></tr>'</span><span>;   </span>
</li>
<li class="alt">
<span class="keyword">for</span><span> ( </span><span class="vars">$i</span><span>=0 ; </span><span class="vars">$i</span><span><10 ; </span><span class="vars">$i</span><span>++) {   </span>
</li>
<li>
<span class="vars">$square</span><span> = </span><span class="vars">$i</span><span> * </span><span class="vars">$i</span><span>;   </span>
</li>
<li class="alt">
<span class="vars">$html</span><span> .= </span><span class="string">'<tr><td>'</span><span> . </span><span class="vars">$i</span><span> . </span><span class="string">'</td><td>'</span><span> . </span><span class="vars">$square</span><span> . </span><span class="string">'</td></tr>'</span><span>;   </span>
</li>
<li><span>}   </span></li>
<li class="alt">
<span class="vars">$html</span><span> .= </span><span class="string">'</table>'</span><span>;  </span>
</li>
<p></p>
<p><strong>3. Use variables in strings </strong></p>
<p>This function allows you to paste without using link symbols Lots of simple strings. PHP allows us to directly include string variables in double-quoted strings. We can find that the processing results of the following two strings are the same. </p>
<p></p>
<pre class="brush:php;toolbar:false"><ol class="dp-c">
<li class="alt"><span><span class="vars">$full_name</span><span> = </span><span class="vars">$first_name</span><span> . </span><span class="string">' '</span><span> . </span><span class="vars">$last_name</span><span>;   </span></span></li>
<li>
<span class="vars">$full_name</span><span> = </span><span class="string">"$first_name $last_name"</span><span>;  </span>
</li>
</ol>
Copy after login

Single quote strings and double quote strings are processed differently in PHP. The content of words in a double-quote string can be parsed and replaced, while the content of words in a single-quote string is always considered to be ordinary characters. For example:

<ol class="dp-c">
<li class="alt"><span><span class="vars">$foo</span><span> = 2;   </span></span></li>
<li>
<span class="func">echo</span><span> </span><span class="string">"foo is $foo"</span><span>; </span><span class="comment">// 打印结果: foo is 2  </span><span> </span>
</li>
<li class="alt">
<span class="func">echo</span><span> </span><span class="string">'foo is $foo'</span><span>; </span><span class="comment">// 打印结果: foo is $foo  </span><span> </span>
</li>
<li>
<span class="func">echo</span><span> </span><span class="string">"foo is $foon"</span><span>; </span><span class="comment">// 打印结果: foo is 2 (同时换行)  </span><span> </span>
</li>
<li class="alt">
<span class="func">echo</span><span> </span><span class="string">'foo is $foon'</span><span>; </span><span class="comment">// 打印结果: foo is $foon </span><span> </span>
</li>
</ol>
Copy after login

As you can see, even the backslash inside a single quote string loses its extended meaning (except adding backslash and add single quotes '). Therefore, when you want to perform variable substitution and include escape sequences such as n (newline) in a string, you should use double quotes.

Single quotation mark strings can be used in more related places. Using single quotation mark strings in scripts (batch files) will be faster for speed reading. The PHP syntax parser should handle single quotation mark strings. It is relatively simple, but the processing of double quotes is more complicated because it also needs to be parsed inside the string, so the processing speed is slightly slower.

Some problems may arise when referencing complex combinations of variables in strings. The following source code will work fine:

<ol class="dp-c">
<li class="alt"><span><span class="func">echo</span><span> </span><span class="string">"value = $foo"</span><span>;   </span></span></li>
<li>
<span class="func">echo</span><span> </span><span class="string">"value = $a[$i]"</span><span>;  </span>
</li>
</ol>
Copy after login

The following source code cannot get the results we want:

<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">"value = $a[$i][$j]"</span><span>; </span><span class="comment">//我们希望打印二维数组$a的某个元素。 </span><span> </span></span></li></ol>
Copy after login

To avoid potential problems in the use of these strings, we usually put Complex variables are separated from the string, like this:

<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">'value = '</span><span> . </span><span class="vars">$a</span><span>[</span><span class="vars">$i</span><span>][</span><span class="vars">$j</span><span>];  </span></span></li></ol>
Copy after login

Another way is to enclose complex variables in curly braces, The grammar parser will correctly identify:

<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">"value = {$a[$i][$j]}"</span><span> </span><span class="comment">//打印二维数组$a的某个元素 </span><span> </span></span></li></ol>
Copy after login

这样,又出现新问题了。当我们想在字串中引用花括号字符本身时,就要记得使用转义符了:

<ol class="dp-c">
<li class="alt"><span><span class="vars">$var</span><span> = 3;   </span></span></li>
<li>
<span class="func">echo</span><span> </span><span class="string">"value = {$var}"</span><span>; </span><span class="comment">// 打印结果 "value = 3"  </span><span> </span>
</li>
<li class="alt">
<span class="func">echo</span><span> </span><span class="string">"value = {$var}"</span><span>; </span><span class="comment">// 打印结果 "value = {3}" </span><span> </span>
</li>
</ol>
Copy after login

三、斜杠和SQL语句

生成HTML源代码或SQL查询语句是编写PHP程序时经常遇到而且是件有趣的问题。为什么这么说呢,应该这涉及到生成另外一种类型的源代码,你必须仔细地考虑和遵循这种源代码所要求的编写语法和规则。

我们来看这样一个CASE,假如你想查询数据库中名字是“O'Keefe”的用户,通常SQL语句的形式是这样的:

<ol class="dp-xml"><li class="alt"><span><span>select * from users where </span><span class="attribute">last_name</span><span> = 'O'</span><span class="attribute-value">Keefe</span><span>'  </span></span></li></ol>
Copy after login

请说明SQL语句这个英语所有格(撇号)需使用反斜杠转义。PHP专门给予了一些函数来处理这样的情况,函数AddSlashes($str)的用途根本就是电子在字串中对引号字符添入反斜杠转义符:

<ol class="dp-c">
<li class="alt"><span><span class="vars">$last_name</span><span> = </span><span class="string">"O'Keefe"</span><span>;   </span></span></li>
<li>
<span class="vars">$sql</span><span> = </span><span class="string">"select * from users where last_name = '"</span><span> . </span><span class="func">addslashes</span><span>(</span><span class="vars">$last_name</span><span>) . </span><span class="string">"'"</span><span>;  </span>
</li>
</ol>
Copy after login

在这个CASE中,你还要在last_name字串外面括上单引号(SQL语法要求),由于那里使用的是双引号串,所以对这对单引号就无须使用转义了。下面的这个语句是使用单引号串的等价形式:

<ol class="dp-c"><li class="alt"><span><span class="vars">$sql</span><span> = </span><span class="string">'select * from users where last_name = ''</span><span> . </span><span class="func">addslashes</span><span>(</span><span class="vars">$last_name</span><span>) . </span><span class="string">'''</span><span>;  </span></span></li></ol>
Copy after login

任何时候你要在数据库中写入字串,你都必须确保里面的引号正确使用了转义符号,这是很多PHP初学者常犯的错误。

四、双引号和HTML

与SQL语句不相同,在标准HTML语言中双引号常被用来表达字串(现在很多浏览器具备较强的容错功能,允许在HTML中用单引号甚至不用引号表达字符串),例如:

<ol class="dp-c">
<li class="alt"><span><span class="vars">$html</span><span> = </span><span class="string">'<a href="'</span><span>.</span><span class="vars">$url</span><span>.</span><span class="string">'">'</span><span>.</span><span class="vars">$link</span><span>.</span><span class="string">'</a>'</span><span>;   </span></span></li>
<li>
<span class="vars">$html</span><span> = </span><span class="string">"<a href="$url">$link</a>"</span><span>;  </span>
</li>
</ol>
Copy after login

HTML语言不支持反斜杠转义,这一点在我们使用列表单的hidden inputs来传输数据的时候就会有所体会了。设置hidden inputs的值的最好办法,是使用htmlspecialchars()函数来编码。下面的语句可以正常传输一个可能包含双引号的数据:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">hidden</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">var</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"<?php echo htmlspecialchars($var) ?>"</span><span class="tag">></span><span>  </span></span></li></ol>
Copy after login

通过本文的介绍,希望对你有帮助。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445809.htmlTechArticle在程序语言中,无论是 单引号 还是 双引号 ,都有很重要的作用,在 PHP 语言中也一样。跟ASP相比,PHP的引号更好用,下面为大家介绍单引...
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