Why would one omit the close tag?

前端 未结 14 1556
旧巷少年郎
旧巷少年郎 2020-11-21 06:18

I keep reading it is poor practice to use the PHP close tag ?> at the end of the file. The header problem seems irrelevant in the following context (and this

14条回答
  •  灰色年华
    2020-11-21 07:03

    Pros

    • Would be logical to close any opened tag, like with other languages. Not only X(HT)ML tags, but as well curly braces, brackets...
    • Less confusing for beginners.

    Cons

    • Avoids headache with adding inadvertently whitespaces after the closing tag, because it breaks the header() function behavior... Some editors or FTP clients / servers are also known to change automatically the end of files (at least, it's their default configuration)
    • PHP manual says closing tag is optional, and Zend even forbids it.

    Conclusion

    I would say that the arguments in favor of omitting the tag look stronger (helps to avoid big headache with header() + it's PHP/Zend "recommendation"). I admit that this isn't the most "beautiful" solution I've ever seen in terms of syntax consistency, but what could be better ?

提交回复
热议问题