


Discuss the difference between single and double quotation marks in PHP programming language_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. 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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
这样,又出现新问题了。当我们想在字串中引用花括号字符本身时,就要记得使用转义符了:
<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>
三、斜杠和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>
请说明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>
在这个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>
任何时候你要在数据库中写入字串,你都必须确保里面的引号正确使用了转义符号,这是很多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>
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>
通过本文的介绍,希望对你有帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.
