为什么有些脚本会省略 PHP 结束标记“?>”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3219383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do some scripts omit the closing PHP tag, '?>'?
提问by Naughty.Coder
In some scripts I see that they omit writing a closing tag ?>for the script. Why is it and should I do this as well?
在某些脚本中,我看到他们省略了?>为脚本编写结束标记。为什么会这样,我也应该这样做吗?
(I'm sure they have not forgotten it.)
(我相信他们没有忘记它。)
采纳答案by dhh
Well, omitting the closing tag is just one solution for avoiding blanks and other characters at the end of file. For example any char which is accidentally added behind the closing tag would trigger an error when trying to modify header info later.
好吧,省略结束标记只是避免文件末尾出现空格和其他字符的一种解决方案。例如,任何意外添加到结束标记后面的字符都会在稍后尝试修改标题信息时触发错误。
Removing the closing tag is kind of "good practice" referring to many coding guidelines.
删除结束标记是一种参考许多编码指南的“良好做法”。
回答by Metalshark
From PHP: Instruction Separation
来自PHP:指令分离
The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using
include()orrequire(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.
文件末尾的 PHP 块的结束标记是可选的,在某些情况下省略它在使用
include()或时很有帮助require(),因此文件末尾不会出现不需要的空格,并且您仍然可以将标题添加到稍后回复。如果您使用输出缓冲,并且不希望在包含文件生成的部分的末尾看到添加的不需要的空格,这也很方便。
回答by brismuth
If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.
如果文件是纯 PHP 代码,则最好省略文件末尾的 PHP 结束标记。这可以防止在 PHP 结束标记后意外添加空格或新行,这可能会导致不必要的影响,因为当程序员无意在脚本中的该点发送任何输出时,PHP 将开始输出缓冲。
回答by Soufiane Hassou
They do it to avoid risking to have whitespaces after the closing tag which may stop headers to work.
他们这样做是为了避免在结束标记之后有空格的风险,这可能会阻止标题工作。
This is, of course, true for PHP-only files.
当然,这对于纯 PHP 文件是正确的。
回答by beawolf
CodeIgniter Frameworksuggests to omit closing tags for
CodeIgniter Framework建议省略关闭标签
"... can cause unwanted output, PHP errors or blank pages".
“... 可能导致不需要的输出、PHP 错误或空白页”。
You can read it here.
你可以在这里阅读。
回答by danidacar
Modern versions of PHP set the output_bufferingflag 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.
现代版本的 PHPoutput_buffering在php.ini. 如果启用输出缓冲,您可以在输出 HTML 后设置 HTTP 头和 cookie,因为返回的代码不会立即发送到浏览器。
Are the examples still valid in this context?
这些例子在这种情况下仍然有效吗?
回答by Nafis Ahmad
- It shows unwanted white space / blank page. HTTP headers do not work for those unwanted whitespace.
- MostJavaScript injection is made at the end of the file. It will show an error message and breaks the code, injected JavaScript code does not get executed.
- 它显示不需要的空白/空白页。HTTP 标头不适用于那些不需要的空格。
- 大多数JavaScript 注入是在文件末尾进行的。它将显示错误消息并破坏代码,注入的 JavaScript 代码不会被执行。

