为什么会省略close标签? [英] Why would one omit the close tag?

查看:161
本文介绍了为什么会省略close标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为在文件末尾使用PHP关闭标记?> 是不好的做法。标题问题在以下上下文中似乎无关紧要(这是迄今为止唯一的好参数):

I keep reading it is poor practice to use the PHP close tag ?> at the end of the file. The header problem seems irrelevant in the following context (and this is the only good argument so far):


现代版本的PHP设置了output_buffering php.ini中的标志
如果启用了输出缓冲,则可以在输出html后设置HTTP标头和cookie,因为返回的代码不会立即发送到浏览器。

Modern versions of PHP set the output_buffering flag in php.ini If output buffering is enabled, you can set HTTP headers and cookies after outputting html because returned code is not sent to the browser immediately.

每个好的练习册和维基都以这个规则开头,但没有人提供充分的理由。
还有另一个跳过结束php标签的好理由吗?

Every good practice book and wiki starts with this 'rule' but nobody offers good reasons. Is there another good reason to skip the ending php tag?

推荐答案

虽然我不记得任何其他原因,比正常课程更早发送标题可能会产生深远的影响。以下是我刚想到的其中一些内容:

While I can't remember any other reason, sending headers earlier than the normal course may have far reaching consequences. Below are just a few of them that happened to come to my mind at the moment:


  1. 虽然当前的PHP版本可能有输出缓存,实际的生产服务器,您将部署代码远比任何开发或测试机器重要。而且他们并不总是倾向于立即关注最新的PHP趋势。

  1. While current PHP releases may have output buffering on, the actual production servers you will be deploying your code on are far more important than any development or testing machines. And they do not always tend to follow latest PHP trends immediately.

您可能会对莫名其妙的功能丢失感到头疼。比如说,您正在实施某种支付网关,并在支付处理器成功确认后将用户重定向到特定的URL。如果出现某种PHP错误,甚至是警告或多余的行结束,付款可能仍未处理,用户可能仍然看似未开单。这也是为什么不必要的重定向是邪恶的原因之一,如果要使用重定向,必须谨慎使用。

You may have headaches over inexplicable functionality loss. Say, you are implementing some kind payment gateway, and redirect user to a specific URL after successful confirmation by the payment processor. If some kind of PHP error, even a warning, or an excess line ending happens, the payment may remain unprocessed and the user may still seem unbilled. This is also one of the reasons why needless redirection is evil and if redirection is to be used, it must be used with caution.

你可能会得到Page加载已取消Internet Explorer中的错误类型,即使在最新版本中也是如此。这是因为 AJAX 响应/ json包含了一些它不应该包含的内容,因为某些PHP文件中有多余的行结尾,就像我几天前遇到的那样。

You may get "Page loading canceled" type of errors in Internet Explorer, even in the most recent versions. This is because an AJAX response/json include contains something that it shouldn't contain, because of the excess line endings in some PHP files, just as I've encountered a few days ago.

如果你的应用中有一些文件下载,它们也会因此而中断。你可能不会注意到它,即使在几年之后,因为下载的具体破坏习惯取决于服务器,浏览器,文件的类型和内容(以及可能的一些其他因素,我不想让你厌烦) 。

If you have some file downloads in your app, they can break too, because of this. And you may not notice it, even after years, since the specific breaking habit of a download depends on the server, the browser, the type and content of the file (and possibly some other factors I don't want to bore you with).

最后,许多PHP框架包括 Symfony Zend 和Laravel(在编码指南,但它遵循诉讼)和 PSR-2标准(第2.2项)要求省略结束标记。 PHP手册本身( 1 2 ), Wordpress Drupal 和我猜的许多其他PHP软件,建议这样做。如果您只是习惯遵循标准(并设置 PHP-CS-Fixer ,你的代码)你可以忘记这个问题。否则你总是需要记住这个问题。

Finally, many PHP frameworks including Symfony, Zend and Laravel (there is no mention of this in the coding guidelines but it follows the suit) and the PSR-2 standard (item 2.2) require omission of the closing tag. PHP manual itself (1,2), Wordpress, Drupal and many other PHP software I guess, advise to do so. If you simply make a habit of following the standard (and setup PHP-CS-Fixer for your code) you can forget the issue. Otherwise you will always need to keep the issue in your mind.

奖金:一些陷阱(实际上是一个)与这两个字符相关:

Bonus: a few gotchas (actually currently one) related to these 2 characters:


  1. 甚至一些知名图书馆可能在之后包含多余的行结尾?> 。一个例子是Smarty,即使是最新版本的2. *和3. *分支都有这个。因此,与往常一样,注意第三方代码。奖金加成:删除不必要的PHP结尾的正则表达式:在包含的所有文件中用空文本替换(\s * \?> \ s *)$ PHP代码。

  1. Even some well-known libraries may contain excess line endings after ?>. An example is Smarty, even the most recent versions of both 2.* and 3.* branch have this. So, as always, watch for third party code. Bonus in bonus: A regex for deleting needless PHP endings: replace (\s*\?>\s*)$ with empty text in all files that contain PHP code.

这篇关于为什么会省略close标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆