uni-app 字符串编码_简化您的编码-HTML和PHP中的字符串处理

uni-app 字符串编码

It should be a truism that simple, clearly written code is easier to maintain than complex and impenetratable code. Yet a common problem in PHP is that many programmers (especially those new to PHP) find some of the program code hard to read. They sometimes compound this error when mixing HTML with PHP and the chief culprit is the "double quote". It is not unusual to see code that looks like this

不言而喻,简单,清晰地编写的代码比复杂且不可渗透的代码更易于维护。 然而,PHP中的一个普遍问题是许多程序员(尤其是那些PHP新手)发现一些程序代码难以阅读。 在将HTML与PHP混合使用时,有时它们会加剧此错误,而罪魁祸首是“双引号”。 看到这样的代码并不稀奇

echo "<input name=\"$someName\" type=\"text\" value=\"$somevalue\">\n";

It is quite easy to lose a double quote somewhere and spend quite a bit of time looking for it or matching pairs. The obvious alternative is to encase the PHP string in single quotes but this affects the PHP variable substitution mechanism which then requires further workarounds. Many people believe that (X)HTML or XML requires double quotes, but the W3C standards are quite clear - either " or ' can be used.

在某处丢失双引号并花费大量时间寻找它或匹配对是很容易的。 显而易见的替代方法是将PHP字符串括在单引号中,但这会影响PHP变量替换机制,因此需要进一步的解决方法。 许多人认为(X)HTML或XML需要双引号,但是W3C标准非常明确-可以使用“或”。

The solution is therefore to enclose strings in double quotes and place HTML attributes in single quotes. This allows the PHP substitution mechanism to function whilst allowing HTML to have its attributes enclosed as required by the standards. Our awkward statement shown above now looks like this

因此,解决方案是将字符串括在双引号中,并将HTML属性放在单引号中。 这允许PHP替换机制起作用,同时允许HTML按照标准要求包含其属性。 我们上面显示的尴尬声明现在看起来像这样

echo "<input name='$someName' type='input' value='$someValue'>\n";

This clearly eliminates a number of sources of confusion by removing the need to escape double quotes, removes the need to use concatenation characters and makes it much easier to match HTML (single) quotes up and it allows variable substitution to function as well. Of course, a problem occurs if you are using javascript in the HTML and the javascript requires parameters to be passed. In this situation the escaped double quotes are back, but their effect is minimal compared to the chaos that can ensue if everything was double quoted. So to extend our example a little further

通过消除对转义双引号的需要,消除了对使用串联字符的需要,并使其与HTML(单)引号的匹配更加容易,并且还允许变量替换起作用,这显然消除了许多混乱的根源。 当然,如果您在HTML中使用javascript并且javascript需要传递参数,则会发生问题。 在这种情况下,转义的双引号又回来了,但是与所有情况都被双引号引起的混乱相比,它们的影响很小。 因此,进一步扩展我们的示例

echo "<input name='$someName' onmouseup='javascript:aFunc(\"prm1\",\"prm2\");' type='input' value='$someValue'>\n";

It is worth trying to keep your code as simple and clear as possible. Remember - if it goes wrong it will probably be you that has to edit it, so make your life as easy as possible.

值得尝试使您的代码尽可能简单明了。 请记住-如果出错,则可能是您必须对其进行编辑,因此使您的生活尽可能轻松。

翻译自: https://www.experts-exchange.com/articles/316/Simplify-your-coding-string-handling-in-HTML-PHP.html

uni-app 字符串编码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值