PHP 编码约定和效率

发布于 2024-11-07 23:32:13 字数 316 浏览 0 评论 0原文

考虑

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

记忆消瘦 2024-11-14 23:32:13

这个问题之前已经得到很好的解决:性能?

简而言之,答案如下:

3 simple rules for you to get it right:

 - No syntax issue can affect performance. Data manipulation does.
 - Speak of performance only backed with results of profiling.
 - Premature optimization is the root of all evil

This question has been addressed quite well before: Opening/closing tags & performance?

In brief, here is the answer:

3 simple rules for you to get it right:

 - No syntax issue can affect performance. Data manipulation does.
 - Speak of performance only backed with results of profiling.
 - Premature optimization is the root of all evil
秉烛思 2024-11-14 23:32:13

可能会影响性能,但这绝不应该成为影响您如何编写良好且可读的代码的点。差异可以忽略不计。

只是我的 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.

影子的影子 2024-11-14 23:32:13

单引号 (') 比双引号 (") 更快,因为解析器不会尝试在字符串中查找变量。
所以,我认为第一个脚本是最好的方法。

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.

ゞ花落谁相伴 2024-11-14 23:32:13

第一个示例会稍微更快,因为使用 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.

○闲身 2024-11-14 23:32:13

如果某些约定调用不同的函数或占用更多空间(因此脚本在物理上占用更多内存空间),则可能会影响性能 - 但这完全可以忽略不计,只是一个偏好问题。如果你将它与 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文