我一直在读,在文件末尾使用PHP关闭标记?>是不好的做法。在下面的上下文中,头文件的问题似乎无关紧要(这是目前为止唯一一个好的参数):

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

每一本好的实践书籍和维基都以这个“规则”开始,但没有人提供好的理由。 还有其他好的理由跳过PHP结束标记吗?


当前回答

除了已经说过的所有内容之外,我还将提出另一个原因,这对我们的调试来说是一个巨大的痛苦。

Apache 2.4.6和php5.4实际上在我们的生产机器上,当关闭PHP标记后面有空白时,分割错误。我只是浪费了好几个小时,直到我终于用strace缩小了虫子的范围。

下面是Apache抛出的错误:

[core:notice] [pid 7842] AH00052: child pid 10218 exit signal Segmentation fault (11)

其他回答

“是否有其他好的理由(除了标题问题)跳过结束php标记?”

在生成二进制输出、CSV数据或其他非html输出时,您不希望无意中输出无关的空白字符。

如果我正确理解这个问题,它与输出缓冲以及这可能对结束/结束标记的影响有关。我不确定这是一个完全合理的问题。问题是输出缓冲区并不意味着所有内容在发送到客户机之前都保存在内存中。意思是有些内容是。

程序员可以故意刷新缓冲区或输出缓冲区那么PHP中的输出缓冲区选项真的会改变结束标记对编码的影响吗?我认为不是这样的。

也许这就是为什么大多数答案都回到了个人风格和语法。

这不是标签……

但如果你有了它,你就有可能在它后面留下空白。

如果你把它作为一个包含在文档的顶部,你可能会在你试图发送HTTP头之前插入空白(即内容),这是不允许的。

嗯,我知道原因,但我不能说出来:

对于只包含PHP代码的文件,永远不会出现结束标记(?>) 允许的。PHP并不要求它, 省略它可以防止 意外注入尾白 空格变成响应。

来源:http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html

比正常进程更早地发送头文件可能会产生深远的后果。下面是我当时碰巧想到的其中一些:

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. 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. 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). 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.

额外的:一些与这两个角色有关的陷阱(实际上目前只有一个):

甚至一些知名的库可能在?>之后包含多余的行尾。一个例子是Smarty,甚至是这两个版本的最新版本。*和3。*分支有这个。因此,与往常一样,要注意第三方代码。额外的奖励:一个用于删除不必要的PHP结尾的正则表达式:在所有包含PHP代码的文件中将(\s*\?>\s*)$替换为空文本。