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

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

问题描述

我一直在读,在文件末尾使用 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 在 php.ini 中设置了 output_buffering 标志如果启用输出缓冲,您可以在输出 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 the 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?

推荐答案

早于正常课程发送标题可能会产生深远的影响.以下只是我目前想到的几个:

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.

您可能会在 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 框架包括 SymfonyZend 和 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.

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

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