PHP 结束标记“?>”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19953483/
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
PHP end tag "?>"
提问by Ari
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 结束标记的有趣现象。我有一个由 Ajax 调用执行的 php 文件。在 php 文件中包含了一个具有各种功能的 php 库文件。当包含这个库时,php 响应包含了一堆空行。当我从库中删除结束标记时,这停止了。谁能向我解释这里发生了什么?
回答by Amal Murali
This is well documented. From the PHP Manual:
这是有据可查的。从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() 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.
文件末尾的 PHP 块的结束标记是可选的,在某些情况下,在使用 include() 或 require() 时省略它是有帮助的,因此文件末尾不会出现不需要的空格,您仍然会能够稍后向响应添加标头。如果您使用输出缓冲,并且不希望在包含文件生成的部分的末尾看到添加的不需要的空格,这也很方便。
Omitting the closing tag helps you prevent accidental whitespace or newlines from being added to the end of the file.
省略结束标记有助于防止在文件末尾添加意外的空格或换行符。
回答by álvaro González
That's a core PHP feature: unlike other languages, you need to tag PHP code with a special tag (normally <?php
) because everything else is considered literal output:
这是 PHP 的核心功能:与其他语言不同,您需要使用特殊标签(通常为<?php
)来标记 PHP 代码,因为其他所有内容都被视为文字输出:
This is not PHP
<?php
echo 'This is PHP' . PHP_EOL;
?>
This is not PHP either
D:\tmp>php test.php
This is not PHP
This is PHP
This is not PHP either
Although the manual mentions HTML, PHP doesn't really know/care what content-type is outside its tags.
尽管手册提到了 HTML,但 PHP 并不真正知道/关心其标签之外的内容类型。
If you forget to close a PHP block when further stuff follows you normally get a syntax error:
如果您忘记关闭 PHP 块,然后其他内容出现,您通常会收到语法错误:
This is not PHP
<?php
echo 'This is PHP' . PHP_EOL;
This is not PHP either
D:\tmp>php test.php
PHP Parse error: syntax error, unexpected 'is' (T_STRING) in D:\tmp\borrame.php on line 6
Blank lines are a sort of special case because they are valid and almost invisible in almost all languages (PHP, HTML, CSS, JavaScript...) so they often unnoticed.
空行是一种特殊情况,因为它们在几乎所有语言(PHP、HTML、CSS、JavaScript...)中都是有效的并且几乎不可见,因此它们经常被忽视。
Once you've removed the ?>
tag, your literal blank lines have disappeared from the script output because they've become part of the PHP code (and, as such, they've started to get ignored).
一旦您删除了?>
标记,您的文字空白行就会从脚本输出中消失,因为它们已经成为 PHP 代码的一部分(因此,它们开始被忽略)。
Of course, blank lines are ignored by PHP but not necessarily by whatever you are generating which, as I said, does not need to be HTML: it can be a picture, a PDF document, an Excel spreadsheet. Bogus white lines can be easily avoided by not closing the last PHP block when it's the last part of the file.
当然,空白行会被 PHP 忽略,但不一定会被您生成的任何内容忽略,正如我所说,它不需要是 HTML:它可以是图片、PDF 文档、Excel 电子表格。当最后一个 PHP 块是文件的最后一部分时,不要关闭它,可以轻松避免虚假的白线。