为什么有时候 php 没有写闭合标签结束符

如题所述

其实不是有时候,而是如果一个文件里面是纯PHP代码(<?php ?>标签外不包含html)的话,是推荐不写结束标签的。

PHP官方手册关于 tag,有以下内容:


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会开启输出缓冲区用来输出内容,而你很可能注意不到。


如果还是不太好理解的话,举个例子:

<?php

    // php code

?>


(这里不是代码,为了让上面的两个换行不被百度知道编辑器移除。)

上面代码 ?> 后面有两个换行符,PHP只解析<?php ?>内的内容,外面的内容直接输出,但是这两个换行符很明显并不是你需要的。如果去掉 ?> 有不会有这个问题存在。

同理,<?php 前面也不要有换行或者空格。


PHP手册相关内容:

http://php.net/manual/en/language.basic-syntax.phptags.php

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网