转义子符,顾名思义:即“\”加数字(一般是8进制数字)来表示。一般我们将其用于转义ASCII码。
但是,转义字符还有另一妙用:用于解决""中的""的表示。笔者近日在学习PHP,就遇到""中有多个"",为了解决此问题,笔者动用了转义字符。例如笔者这段代码:
print "<p>You have selected to purchase: <br/>
<span class=\"number\">$quantity</span> widget(s) at <br/>
$<span class=\"number\">$price</span> price each plus a <br/>
$<span class=\"number\">$shipping</span> shipping cost and a <br/>
<span class=\"number\">$tax</span> percent tax rate. <br/>
After your $<span class=\"number\">$discount</span> discount, the total cost is
$<span class=\"number\">$total</span>. <br/>
Divided over <span class=\"number\"> $payments</span> monthly payments, that would be $<span class=\"number\">$monthly </span> each.</p>";
笔者就动用了多个转义字符\"来表示",因此笔者的class的css:number得以实现。更甚的一个例子是:print"The total is $$total";$$total
这样的两个美元符号会产生一个非常复杂的变量类型,因此修改如下:print"The total is \$$total";
在此,笔者也要提示:如何避免被识别为转义字符?解决方案如下:在编写网页时,如果直接在双引号之间输入路径,其中“\”及其之后的文本易被误认为转义字符。为避免这一点,只需在字符串的引号前加“@”符号(不包括外侧引号)。如:@"c:\ab\cd\ef.jpg"。