PHP结束标签“?"

2022-01-18 00:00:00 tags php

我遇到了一个有趣的现象,即 PHP 结束标记.我有一个由 Ajax 调用执行的 php 文件.在 php 文件中包含一个具有各种功能的 php 库文件.当包含这个库时,php 响应包含一堆空行.当我从库中删除结束标记时,这种情况就停止了.谁能给我解释一下这里发生了什么?

I've had an interesting phenomenon with a PHP end tag. I had a php file that was executed by an Ajax call. In the php file was included a php library file with assorted functions. When this library was included the php response included a bunch of blank lines. When I removed the end tag from the library this stopped happening. Can anyone explain to me what going on here ?

推荐答案

这是有据可查的.来自 PHP 手册:

This is well documented. From the PHP Manual:

文件末尾的 PHP 块的结束标记是可选的,在某些情况下,在使用 include() 或 require() 时省略它会很有帮助,因此文件末尾不会出现不需要的空格,并且稍后您仍然可以向响应中添加标头.如果您使用输出缓冲,它也很方便,并且不希望在包含的文件生成的部分末尾看到添加的不需要的空白.

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() or require(), 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.

省略结束标记有助于防止意外的空格或换行符被添加到文件末尾.

Omitting the closing tag helps you prevent accidental whitespace or newlines from being added to the end of the file.

相关文章