为什么有些脚本省略了结束PHP标记'?>'?(复制)

时间:2022-10-06 10:32:56

This question already has an answer here:

这个问题已经有了答案:

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

(我肯定他们没有忘记。)

7 个解决方案

#1


167  

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.

省略结束标记只是避免在文件末尾出现空格和其他字符的一种解决方案。例如,在结束标记后面添加的任何char都会在稍后尝试修改头信息时触发错误。

Removing the closing tag is kind of "good practice" referring to many coding guidelines.

删除结束标记是一种“良好的实践”,指的是许多编码指南。

#2


78  

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() 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块的结束标签的文件是可选的,在某些情况下省略使用包括时是很有帮助的()或需要(),所以不需要的空格不会出现在最终的文件,你仍然可以添加标题后的响应。如果您使用输出缓冲,并且不希望在包含的文件生成的部分的末尾看到不需要的空格,那么这也很方便。

#3


31  

php.net on PHP tags:

php.net PHP标签:

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将启动输出缓冲。

#4


14  

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文件来说是正确的。

#5


7  

CodeIgniter Framework suggests to omit closing tags for

CodeIgniter框架建议省略关闭标签

"... can cause unwanted output, PHP errors or blank pages".

“…可能导致不需要的输出、PHP错误或空白页。

You can read it here.

你可以在这里读。

#6


5  

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的现代版本在PHP .ini中设置了output_buffering标志。如果启用了输出缓冲,那么可以在输出HTML之后设置HTTP头和cookie,因为返回的代码不会立即发送到浏览器。

Are the examples still valid in this context?

这些例子在这个上下文中仍然有效吗?

#7


0  

  1. It shows unwanted white space / blank page. HTTP headers do not work for those unwanted whitespace.
  2. 它显示不需要的空白页。HTTP头对那些不需要的空格不起作用。
  3. Most JavaScript 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.
  4. 大多数JavaScript注入都是在文件的末尾进行的。它会显示一个错误消息并破坏代码,注入的JavaScript代码不会被执行。

#1


167  

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.

省略结束标记只是避免在文件末尾出现空格和其他字符的一种解决方案。例如,在结束标记后面添加的任何char都会在稍后尝试修改头信息时触发错误。

Removing the closing tag is kind of "good practice" referring to many coding guidelines.

删除结束标记是一种“良好的实践”,指的是许多编码指南。

#2


78  

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() 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块的结束标签的文件是可选的,在某些情况下省略使用包括时是很有帮助的()或需要(),所以不需要的空格不会出现在最终的文件,你仍然可以添加标题后的响应。如果您使用输出缓冲,并且不希望在包含的文件生成的部分的末尾看到不需要的空格,那么这也很方便。

#3


31  

php.net on PHP tags:

php.net PHP标签:

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将启动输出缓冲。

#4


14  

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文件来说是正确的。

#5


7  

CodeIgniter Framework suggests to omit closing tags for

CodeIgniter框架建议省略关闭标签

"... can cause unwanted output, PHP errors or blank pages".

“…可能导致不需要的输出、PHP错误或空白页。

You can read it here.

你可以在这里读。

#6


5  

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的现代版本在PHP .ini中设置了output_buffering标志。如果启用了输出缓冲,那么可以在输出HTML之后设置HTTP头和cookie,因为返回的代码不会立即发送到浏览器。

Are the examples still valid in this context?

这些例子在这个上下文中仍然有效吗?

#7


0  

  1. It shows unwanted white space / blank page. HTTP headers do not work for those unwanted whitespace.
  2. 它显示不需要的空白页。HTTP头对那些不需要的空格不起作用。
  3. Most JavaScript 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.
  4. 大多数JavaScript注入都是在文件的末尾进行的。它会显示一个错误消息并破坏代码,注入的JavaScript代码不会被执行。