PHP 编码约定和效率
考虑
<a href="<?php echo $url;?>"><?php echo $name;?></a>
并比较,
<?php echo "<a href=\"{$url}\">{$name}</a>";?>
然后在同一页面上考虑数百个不同变体的内容。
其中一个约定是否会影响 以任何方式表现还是只是一个偏好问题?
Consider
<a href="<?php echo $url;?>"><?php echo $name;?></a>
and compare with
<?php echo "<a href=\"{$url}\">{$name}</a>";?>
then consider hundreds of these in different variations on the same page.
Does one or the other convention affect
performance in any way or is it just a matter of preference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个问题之前已经得到很好的解决:性能?
简而言之,答案如下:
This question has been addressed quite well before: Opening/closing tags & performance?
In brief, here is the answer:
它可能会影响性能,但这绝不应该成为影响您如何编写良好且可读的代码的点。差异可以忽略不计。
只是我的 2 美分:如果我有大量的 html 并且我只想集成一些变量,第一个更具可读性。如果第二个只有一行或其他内容,则更具可读性。
It may affect the performance, but that should never be the point to influence how you write good and readable code. The difference is negligible.
Just my 2 cent: The first one is much more readable, if I have huge parts of html and I just want to integrate some variables. The second one is more readable, if its only a single line or something.
单引号 (') 比双引号 (") 更快,因为解析器不会尝试在字符串中查找变量。
所以,我认为第一个脚本是最好的方法。
The single quotes (') is more faster than double quotes ("), because the parser don't try to find variables in the string.
So, I think that the first script is the best way.
第一个示例会稍微更快,因为使用 PHP 回显内容会比仅使用原始 html 显示文本增加微小的延迟。
然而,这种差异根本不明显,我建议您选择您喜欢的。
The first example will be slightly faster because using PHP to echo content adds a tiny latency over just displaying text using raw html.
However, this difference is not noticeable at all and I would advise you to use choose whichever you prefer.
如果某些约定调用不同的函数或占用更多空间(因此脚本在物理上占用更多内存空间),则可能会影响性能 - 但这完全可以忽略不计,只是一个偏好问题。如果你将它与 Smarty 或其他模板系统之类的东西进行比较 - 另一方面会影响性能。
Some conventions can possibly effect performance if they are calling different functions or if they take up more space (so the script physically takes up more space in memory) - but this is completely negligible and just a matter of preference. If you compare it to something like Smarty or another Templating system - that on the other hand will effect performance.