简体   繁体   English

封装评论的好处和注意事项 <?php /* */ ?> 在HTML页面?

[英]Benefits and caveats of encapsulating comments within <?php /* */ ?> in an HTML page?

I recently came across some code that looks something like this: 我最近遇到了一些看起来像这样的代码:

<head>
   <?php /* The following scripts are required */ ?>
   <script type="text/javascript" src="script.js"></script>
</head>

where the PHP consists only of a comment within an HTML document. PHP 包含HTML文档中的注释。

I imagine this was done to prevent the comment being visible in the source code of the page, and to make the resultant HTML page lighter. 我想这样做是为了防止评论在页面的源代码中可见,并使得结果HTML页面更轻松。 Clever. 聪明。

However, I wonder what the performance impact, or any other notable caveat, of invoking the PHP engine for nothing is. 但是,我想知道对于什么都没有调用PHP引擎的性能影响或任何其他值得注意的警告。

The performance impact is so incredibly tiny you don't need to worry about it. 性能影响非常小,您无需担心它。 Any impact would be offset by the bandwidth you save by not sending HTML comments to your user. 通过不向用户发送HTML注释,您所节省的带宽将抵消任何影响。 On a very busy website, that would be a saving worth having. 在一个非常繁忙的网站上,这将是值得拥有的。

Good question! 好问题!

A set up a test case to work out what the hit would be. 设置一个测试用例来确定命中的内容。 The test scripts is: 测试脚本是:

<head>
   <!-- The following scripts are required -->
   <script type="text/javascript" src="script.js"></script>
   <!-- The following scripts are required -->
   <script type="text/javascript" src="script.js"></script>
   <!-- The following scripts are required -->
   <script type="text/javascript" src="script.js"></script>
   <!-- The following scripts are required -->
   <script type="text/javascript" src="script.js"></script>
   <!-- The following scripts are required -->
   <script type="text/javascript" src="script.js"></script>
   <!-- The following scripts are required -->
   <script type="text/javascript" src="script.js"></script>
   <!-- The following scripts are required -->
   <script type="text/javascript" src="script.js"></script>
   <!-- The following scripts are required -->
   <script type="text/javascript" src="script.js"></script>
   <!-- The following scripts are required -->
   <script type="text/javascript" src="script.js"></script>
</head>

And then the same but replacing HTML with PHP comments. 然后相同但用PHP注释替换HTML。 I used 10 comments to make the maths easier - and it's probably in the right ball-park. 我用10条评论让数学更容易 - 而且它可能在正确的球场。

10000 requests took 9.117546 seconds using HTML comments, and 10.92784 seconds using PHP comments. 使用HTML注释,10000个请求占用9.117546秒,使用PHP注释占用10.92784秒。 The difference being 1.81029 seconds, making it 0.000181029 seconds per page load quicker for HTML meaning that the average page load was 1/10000th of a second slower using PHP to hide the comments. 差异为1.81029秒,使得HTML每页加载0.000181029秒更快,这意味着使用PHP隐藏评论时平均页面加载速度为1/10000秒。

The data transfered was 5.55MB for the PHP comments and 11.4MB for HTML - thought it must be noted here that the difference artificially high because there's no HTML content. PHP评论传输的数据为5.55MB,HTML的传输数据为11.4MB - 认为必须注意的是人为差异,因为没有HTML内容。

You can see the full results of the tests here: http://slightlymore.co.uk/html_comments.txt and http://slightlymore.co.uk/php_comments.txt 您可以在此处查看测试的完整结果: http//slightlymore.co.uk/html_comments.txthttp://slightlymore.co.uk/php_comments.txt

In short, the hit on performance in PHP is negligible, but saved bandwidth will pay off much more. 简而言之,PHP中的性能打击可以忽略不计,但节省的带宽将获得更多回报。

Everyone else essentially says "it makes very little difference". 其他人基本上都说“它差别很小”。 This is a correct observation for a couple of infrastructure templates: where PHP is delivered through mod_php5 or one of the true FastCGI implementations. 这是对几个基础架构模板的正确观察:PHP通过mod_php5或其中一个真正的FastCGI实现提供。 However this is certainly not the case on a shared hosting account where account separation is enforced by UID and PHP is delivered by a php-cgi activation per request . 然而在UID强制执行帐户分离并且每个请求通过php-cgi激活提供PHP的共享主机帐户上肯定不是这种情况 This is approx 100mSec CPU over head per request whereas Apache serving an HTML page is a few mSec. 这是每个请求大约100毫秒CPU头,而服务HTML页面的Apache是​​几毫安。

A factor of 30x or so in CPU load on such requests is not "it makes very little difference". 在这些请求上CPU负载的30倍左右不是“它会产生很小的差异”。

The performance hit would not be seen by clients, nor really on the server. 性能不会被客户看到,也不会出现在服务器上。 I can't imagine that the performance difference would be measurable. 我无法想象性能差异是可测量的。

封装在<!-- -->注释将被发送到客户端, <?php ?>注释将不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM