单引号、双引号与定界符——PHP
<p><span style="font-size: 18pt"><strong><span style="background-color: rgba(192, 192, 192, 1)"><span style="font-family: 幼圆">单引号与双引号</span></span></strong></span></p><p> </p>
<p><span style="font-size: 18px"><strong><span style="font-family: 幼圆; background-color: rgba(204, 255, 255, 1)"><span style="line-height: 28px">单引号和双引号在echo输出时的区别</span></span></strong></span></p>
<p><span style="font-family: 幼圆; font-size: 18px">echo输出时,如果使用单引号,那么echo会把单引号之间的全部内容当成普通字符串输出,不能识别变量和转义字符(<span style="text-decoration: underline">单引号串中的内容总被认为是普通字符</span>)</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(128, 0, 128, 1)">$str1</span> = "Hello"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> '$str1<br/>';</span></pre>
</div>
<p><span style="font-family: 幼圆; font-size: 18px">运行结果:</span></p>
<p> <img src="https://img2018.cnblogs.com/blog/1590962/201910/1590962-20191021202206168-1092927326.jpg" alt=""></p>
<p> </p>
<p><span style="font-family: 幼圆; font-size: 18px">echo输出时,如果使用双引号,那么echo会识别双引号之间的变量和转义字符</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(128, 0, 128, 1)">$str1</span> = "Hello"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> '$str1<br/>'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> "<span style="color: rgba(128, 0, 128, 1)">$str1</span><br/>";</span></pre>
</div>
<p><span style="font-family: 幼圆"><span style="font-size: 18.6667px; line-height: 28px">运行结果:</span></span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18.6667px; line-height: 28px"><img src="https://img2018.cnblogs.com/blog/1590962/201910/1590962-20191021202549176-607635396.jpg" alt=""></span></span></p>
<p><span style="font-family: 幼圆; font-size: 18px"><span style="line-height: 28px">在上面的测试代码中,给$str1变量赋值的内容只有普通字符,所以使用单引号和双引号赋值的作用时一样的</span></span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18.6667px; line-height: 28px">------------------------------------------------------</span></span></p>
<p><span style="font-size: 18px"><strong><span style="font-family: 幼圆; background-color: rgba(204, 255, 255, 1)"><span style="line-height: 28px">单引号和双引号在给变量赋值时的区别</span></span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(128, 0, 128, 1)">$str1</span> = "Hello"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(128, 0, 128, 1)">$str2</span> = '$str1 my friend'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(128, 0, 128, 1)">$str3</span> = "<span style="color: rgba(128, 0, 128, 1)">$str1</span> my friend"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> "<span style="color: rgba(128, 0, 128, 1)">$str2</span><br/>"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> "<span style="color: rgba(128, 0, 128, 1)">$str3</span><br/>";</span></pre>
</div>
<p><span style="font-family: 幼圆; font-size: 18px"><span style="line-height: 28px">运行结果:</span></span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18.6667px; line-height: 28px"><img src="https://img2018.cnblogs.com/blog/1590962/201910/1590962-20191021203854224-841166520.jpg" alt=""></span></span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18.6667px; line-height: 28px">-----------------------------------------------------</span></span></p>
<p><span style="font-family: 幼圆; font-size: 18px">PHP语法分析器是用成对的引号来判断一个字符串的。因此,所有字符串必须使用同一种单或者双引号来定义开始和结束,也就是说可以在双引号中包含单引号,单引号中包含双引号</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 双引号中包含单引号 和 单引号中包含双引号 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(128, 0, 128, 1)">$str6</span> = "Maybe today 'maybe tomorrow'"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(128, 0, 128, 1)">$str7</span> = 'Maybe today "maybe tomorrow"'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> "<span style="color: rgba(128, 0, 128, 1)">$str6</span><br/>"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> "<span style="color: rgba(128, 0, 128, 1)">$str7</span><br/>";</span></pre>
</div>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 27px">运行结果:</span></span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 27px"><img src="https://img2018.cnblogs.com/blog/1590962/201910/1590962-20191021204709331-1857735228.jpg" alt=""></span></span></p>
<p> </p>
<p> </p>
<p><span style="font-family: 幼圆"><span style="font-size: 18.6667px; line-height: 28px">如果想在单引号中包含单引号,双引号中包含双引号,则需要使用转义符\</span></span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 在双引号中包含双引号 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(128, 0, 128, 1)">$str8</span> = "Who How What \"Why\" No"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> "<span style="color: rgba(128, 0, 128, 1)">$str8</span><br/>"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">4</span>
<span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 在单引号中包含单引号 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(128, 0, 128, 1)">$str9</span> = 'Who How What \'Why\' No'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">7</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> "<span style="color: rgba(128, 0, 128, 1)">$str9</span><br/><br/>";</span></pre>
</div>
<p><span style="font-family: 幼圆"><span style="font-size: 18.6667px; line-height: 28px">运行结果:</span></span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18.6667px; line-height: 28px"><img src="https://img2018.cnblogs.com/blog/1590962/201910/1590962-20191021205135514-1407474650.jpg" alt=""></span></span></p>
<p> </p>
<p> </p>
<p><span style="background-color: rgba(192, 192, 192, 1)"><strong><span style="font-size: 18pt"><span style="font-family: 幼圆"><span style="line-height: 28px">定界符</span></span></span></strong></span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 28px">使用格式:</span></span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <<<<span style="color: rgba(0, 0, 0, 1)">EOF
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 0, 1)">`````
</span><span style="color: rgba(0, 128, 128, 1)">3</span> EOF;</span></pre>
</div>
<p> </p>
<p><span style="font-family: 幼圆; font-size: 18px"><span style="line-height: 28px">简单来说定界符的作用有:</span></span></p>
<p><span style="font-size: 18px; font-family: 幼圆">1.PHP定界符的作用就是按照原样,包括换行格式什么的,输出在其内部的东西</span></p>
<p><span style="font-size: 18px; font-family: 幼圆">2.在PHP定界符中的任何特殊字符都不需要转义</span></p>
<p><span style="font-size: 18px; font-family: 幼圆">3.PHP定界符中的PHP变量会被识别,并用变量本身的值替换</span></p>
<p><span style="font-size: 18px; font-family: 幼圆">(从上面三条可以看出定界符和双引号非常相似)</span></p>
<p> </p>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 20.7692px">使用定界符注意事项:</span></span></p>
<p><span style="font-size: 18px; font-family: 幼圆">1、</span><span style="font-family: 幼圆; font-size: 18px">EOF 可以用任意其它字符代替,只需保证结束标识与开始标识一致,并且开始标记和结束标记不在正文中出现即可</span><span style="font-size: 18px; font-family: 幼圆">(意思是可以写成如下形式)</span></p>
<div class="cnblogs_code">
<pre><<<<span style="color: rgba(0, 0, 0, 1)">TODAY
`````
TODAY;</span></pre>
</div>
<p> </p>
<p><span style="font-size: 18px; font-family: 幼圆">2、结束标识必须顶格独自占一行(即必须从行首开始,前后不能衔接任何空白和字符)</span></p>
<p><strong><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 20.7692px">正确的示例:</span></span></strong></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(128, 0, 128, 1)">$str1</span> = 'Hello'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <<<<span style="color: rgba(0, 0, 0, 1)">EOF
</span><span style="color: rgba(0, 128, 128, 1)">3</span> {<span style="color: rgba(128, 0, 128, 1)">$str1</span><span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">4</span> EOF;</span></pre>
</div>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 20.7692px">运行结果:</span></span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 20.7692px"><img src="https://img2018.cnblogs.com/blog/1590962/201910/1590962-20191022162309675-1082278119.jpg" alt=""></span></span></p>
<p> </p>
<p> </p>
<p><strong><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 20.7692px">错误的示例:</span></span></strong></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(128, 0, 128, 1)">$str1</span> = 'Hello'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <<<<span style="color: rgba(0, 0, 0, 1)">EOF
</span><span style="color: rgba(0, 128, 128, 1)">3</span> {<span style="color: rgba(128, 0, 128, 1)">$str1</span><span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">4</span>EOF;</span></pre>
</div>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 20.7692px">运行结果:</span></span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 20.7692px"><img src="https://img2018.cnblogs.com/blog/1590962/201910/1590962-20191022162448275-932851251.jpg" alt=""></span></span></p>
<p> </p>
<p><span style="font-size: 18px; font-family: 幼圆">3、开始标识可以不带引号或带单双引号,<strong>不带引号与带双引号效果一致</strong>,解释内嵌的变量和转义符号,带单引号则不解释内嵌的变量和转义符号</span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 20.7692px">第一部分:<span style="background-color: rgba(204, 255, 255, 1)"><strong>不带引号与带双引号效果一致</strong></span></span></span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(128, 0, 128, 1)">$str1</span> = 'Hello'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <<<<span style="color: rgba(0, 0, 0, 1)">EOF
</span><span style="color: rgba(0, 128, 128, 1)">3</span> {<span style="color: rgba(128, 0, 128, 1)">$str1</span><span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 0, 1)">EOF;
</span><span style="color: rgba(0, 128, 128, 1)">5</span>
<span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(128, 0, 128, 1)">$str2</span> = 'Hi'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">7</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <<<"EOF"
<span style="color: rgba(0, 128, 128, 1)">8</span> {<span style="color: rgba(128, 0, 128, 1)">$str2</span><span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">9</span> EOF;</span></pre>
</div>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 20.7692px">运行结果:</span></span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 20.7692px"><img src="https://img2018.cnblogs.com/blog/1590962/201910/1590962-20191022164632088-238234538.jpg" alt=""></span></span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 20.7692px">第二部分:<span style="background-color: rgba(204, 255, 255, 1)"><strong>带单引号则不解释内嵌的变量和转义符号</strong></span></span></span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(128, 0, 128, 1)">$str1</span> = 'Hello'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <<<'EOF'
<span style="color: rgba(0, 128, 128, 1)">3</span> {<span style="color: rgba(128, 0, 128, 1)">$str1</span><span style="color: rgba(0, 0, 0, 1)">}\n
</span><span style="color: rgba(0, 128, 128, 1)">4</span> EOF;</span></pre>
</div>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 20.7692px">运行结果:</span></span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 20.7692px"><img src="https://img2018.cnblogs.com/blog/1590962/201910/1590962-20191022170543950-2001285876.jpg" alt=""></span></span></p>
<p> </p>
<p><span style="font-size: 18px; font-family: 幼圆">4、当内容需要内嵌引号(单引号或双引号)时,不需要加转义符,本身对单双引号转义</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(128, 0, 128, 1)">$str1</span> = 'Hello'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <<<<span style="color: rgba(0, 0, 0, 1)">EOF
</span><span style="color: rgba(0, 128, 128, 1)">3</span> {<span style="color: rgba(128, 0, 128, 1)">$str1</span>}"Hi"'Yo'
<span style="color: rgba(0, 128, 128, 1)">4</span> EOF; </span></pre>
</div>
<p><span style="font-family: 幼圆; font-size: 18px">运行结果: </span></p>
<p><img src="https://img2018.cnblogs.com/blog/1590962/201910/1590962-20191022193308928-341602115.jpg" alt=""></p>
<p> </p>
<p> </p>
<p><span style="font-size: 18pt; background-color: rgba(192, 192, 192, 1)"><strong><span style="font-family: 幼圆">定界符与双引号</span></strong></span></p>
<p><span style="font-family: 幼圆; font-size: 18px"> 当需要用echo输出大量的html和js代码时,定界符的优势便体现出来了,以html为例,当echo输出的html代码中含有单引号或双引号时,如果使用普通的双引号输出,则需要手动的添加转义符(\)来保证语法不会出错,如果使用定界符则不需要手动添加转义符,测试代码如下</span></p>
<p> </p>
<p><strong><span style="background-color: rgba(204, 255, 255, 1)"><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 24.5455px">使用echo""输出html代码</span></span></span></strong></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> "<a href=\"https://www.baidu.com\">此连接使用了href属性</a><br/>";</span></pre>
</div>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 24.5455px">运行结果:</span></span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 24.5455px"><img src="https://img2018.cnblogs.com/blog/1590962/201910/1590962-20191022195915838-1266699923.jpg" alt=""></span></span></p>
<p><span style="font-family: 幼圆; font-size: 18px">在上面代码中,如果不对html代码中的双引号添加转义符,则会报错</span></p>
<p><img src="https://img2018.cnblogs.com/blog/1590962/201910/1590962-20191022200100196-465275057.jpg" alt=""></p>
<p> </p>
<p><span style="background-color: rgba(204, 255, 255, 1)"><strong><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 24.5455px">使用echo定界符输出html代码</span></span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <<<<span style="color: rgba(0, 0, 0, 1)">EOF
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <a href="https://www.baidu.com">此连接使用了href属性</a><br/>
<span style="color: rgba(0, 128, 128, 1)">3</span> EOF;</span> </pre>
</div>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 24.5455px">运行效果:</span></span></p>
<p><span style="font-family: 幼圆"><span style="font-size: 18px; line-height: 24.5455px"><img src="https://img2018.cnblogs.com/blog/1590962/201910/1590962-20191022200636078-331768131.jpg" alt=""></span></span></p>
<p> </p>
<p>————————————————————————————</p>
<p><span style="font-family: 幼圆; font-size: 18px">编辑器:Notepad++</span></p>
<p><span style="font-family: 幼圆; font-size: 18px">环境:WampServer Version 3.1.9 64bit</span></p>
<p><span style="font-family: 幼圆; font-size: 18px">PHP Version:7.3.5 </span></p><br><br>
来源:https://www.cnblogs.com/lanhaicode/p/11703500.html
頁:
[1]