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

Jul 20, 2016 am 10:56 AM
php the difference apostrophe pair exist quotation marks still

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的引号更好用,下面为大家介绍单引...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

The difference between Ether and Bitcoin What is the difference between Ether and Bitcoin The difference between Ether and Bitcoin What is the difference between Ether and Bitcoin Mar 19, 2025 pm 04:54 PM

The difference between Ethereum and Bitcoin is significant. Technically, Bitcoin uses PoW, and Ether has shifted from PoW to PoS. Trading speed is slow for Bitcoin and Ethereum is fast. In application scenarios, Bitcoin focuses on payment storage, while Ether supports smart contracts and DApps. In terms of issuance, the total amount of Bitcoin is 21 million, and there is no fixed total amount of Ether coins. Each security challenge is available. In terms of market value, Bitcoin ranks first, and the price fluctuations of both are large, but due to different characteristics, the price trend of Ethereum is unique.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

What is the function of C language sum? What is the function of C language sum? Apr 03, 2025 pm 02:21 PM

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

Which is better PHP or Laravel? Which is better PHP or Laravel? Mar 27, 2025 pm 05:31 PM

PHP and Laravel are not directly comparable, because Laravel is a PHP-based framework. 1.PHP is suitable for small projects or rapid prototyping because it is simple and direct. 2. Laravel is suitable for large projects or efficient development because it provides rich functions and tools, but has a steep learning curve and may not be as good as pure PHP.

Is there any mobile app that can convert XML into PDF? Is there any mobile app that can convert XML into PDF? Apr 02, 2025 pm 08:54 PM

An application that converts XML directly to PDF cannot be found because they are two fundamentally different formats. XML is used to store data, while PDF is used to display documents. To complete the transformation, you can use programming languages ​​and libraries such as Python and ReportLab to parse XML data and generate PDF documents.

See all articles