Why does phpBB end documents with ?> ?

Discussion of general topics related to the new version and its place in the world. Don't discuss new features, report bugs, ask for support, et cetera. Don't use this to spam for other boards or attack those boards!
Forum rules
Discussion of general topics related to the new release and its place in the world. Don't discuss new features, report bugs, ask for support, et cetera. Don't use this to spam for other boards or attack those boards!
Post Reply
Roberdin
Registered User
Posts: 1546
Joined: Wed Apr 09, 2003 8:44 pm
Location: London, United Kingdom

Why does phpBB end documents with ?> ?

Post by Roberdin »

I recently discovered that, in documents that only contain PHP, it is not only syntactically valid -- but recommended that the document not close with ?>. This is of course normally required to terminate a code block, but allowed here precisely because some editors throw in additional new lines, which may cause issues if header output is to follow. Considering that this is a common source of consternation when modifying phpBB files for new users, I wondered if the developers would mind helping me understand why they decided not to follow this convention?

Thanks. =]
Rob
User avatar
naderman
Consultant
Posts: 1727
Joined: Sun Jan 11, 2004 2:11 am
Location: Berlin, Germany
Contact:

Re: Why does phpBB end documents with ?> ?

Post by naderman »

I'm not sure why it was originally done this way. It won't be changed in the stable release since it's not exactly something that needs to be changed. But it's very likely that future releases will no longer end files with ?>.
TerryE
Registered User
Posts: 95
Joined: Sat May 23, 2009 12:24 am
Contact:

Re: Why does phpBB end documents with ?> ?

Post by TerryE »

This codicil is really for other browsing readers rather than the posters above who will be familiar with this.

This isn't a widely discussed feature or issue of PHP. The only reference that I can find in the php.net manual is in the section Language Reference->Basic Syntax->Instruction separation which contains the note:
PHP Manual wrote:Note: 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.
The online discussion makes no expansion of this issue. I have found references such as the recent Open Core blog entry for: PHP files – Closing the PHP tag or not? and the section B.2.1 (PHP File Formatting, General) of the Zend Framework Programmer's Reference Guide which states more explicitly:
Zend Framework PRM wrote:For files that contain only PHP code, the closing tag ("?>") is never permitted. It is not required by PHP, and omitting it prevents the accidental injection of trailing white space into the response.
and cons such as this blog entry. I have observed that most of the PHP modules in the pbpBB releases end with ?> (though some end with ?>\n). I have also found that many editors (such as vi) will add the trailing newline automatically so that when you diff edited code, this generates \No newline found differences.

Note that this trailing newline is suppressed by the PHP RTS, so <?php ... ?>\n does not emit a newline character. However IMHO this is a piece of undocumented sugar, and for that reason I would think that the preferable standard should be that <?php should be the first 5 bytes of any code only module, and that the ?> should be omitted, as this is explicitly permitted in the LRM and removes any doubt over potential whitespace generation.

PS. I have a working knowledge of a few dozen languages. I've used about a dozen professionally and a similar number of assembly languages in my 35 year IT degree. I really only started using PHP in anger a couple of years ago when working with LAMP stacks and applications such as phpBB and Wikimedia, so I wouldn't regard myself as a PHP expert.
Roberdin
Registered User
Posts: 1546
Joined: Wed Apr 09, 2003 8:44 pm
Location: London, United Kingdom

Re: Why does phpBB end documents with ?> ?

Post by Roberdin »

It is a convention recommended by Zend, more than anyone else; in fact, it was questioned in the Zend Certified Engineer mock exams. (I'm not contractually allowed to tell you if it was in the real exam or not. ;)) I suspect this is because Zend are used to dealing with the very large, business-logic-separated web applications (a bit like phpBB :D), whereas it tends to be the unfortunate case to find most PHP applications are not at all written like that or phpBB (lame): there is a lot of dropping in and out of PHP code, which might be easier to follow if code blocks are each terminated. With entirely PHP files, perhaps it's not fair to think of a giant code block in an "otherwise" plain XML-complaint HTML file.

I think the drawback: "this is lazy coding, a programmer should know what goes at the end of his files" is somewhat arbitrary. As long as we have a clear standard, then it's not lazy. And of course, for phpBB, the primary issue is mods implemented by less experienced programmers.

Having looked at some of the newer phpBB 3.1 code today, I think it fits in perfectly with the ethos which now seems to be there in the code: "Help the mod authors." I was particularly impressed by the method employed to prevent anyone using the request/server variables in the $_GLOBALS array. :)
Rob
Post Reply