小小转义字符另一妙用

转义子符,顾名思义:即“\”加数字(一般是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>"; 


笔者就动用了多个转义字符\"来表示",因此笔者的classcssnumber得以实现。更甚的一个例子是:print"The total is $$total";$$total

 这样的两个美元符号会产生一个非常复杂的变量类型,因此修改如下:print"The total is \$$total";

   在此,笔者也要提示:如何避免被识别为转义字符?解决方案如下:在编写网页时,如果直接在双引号之间输入路径,其中“\”及其之后的文本易被误认为转义字符。为避免这一点,只需在字符串的引号前加“@”符号(不包括外侧引号)。如:@"c:\ab\cd\ef.jpg"  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值