Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm a reasonably competent programmer by most standards and I was burned by PHP once.

If you include a file with their include() function and have whitespace after the closing ?> tag in the file--PHP will throw an error.

Except it gets better: whether this happens or not depends on a setting in php.ini (output buffering I think).

This happens if you're trying to send header information from the parent file but include a file that outputs whitespace on accident. The output buffering delays sending the output (because it's buffered, duh :)) so the header info can be sent, hence no error.

This is one of those things I didn't know and wish I did.

http://stackoverflow.com/questions/1793482/php-error-cannot-...

The work around: omit the closing ?> tag from included files.



That's one of the most irritating problems with PHP due to the impossibility to find the bug (well, it's a whitespace, it's not as if one could see it). On my server, including a file with a whitespace after the ?> actually made the server send the PHP source code as text, including the mysql passwords that were included in the file.


Ouch, I generally try to put password files in directories not even in www_root, that way if the web server config gets messed up this stuff isn't even possible.


Most PHP dev's omit the closing tag anyway for a variety of reasons (mostly because, well, it's not needed)


To a non-PHP programmer it's pretty non-intuitive to omit a closing tag. Most programmers are trained to balance things like tags, parentheses, and curly braces.


"non-intuitive"? I would say "horrific", "disgusting" or something on these lines.


I sort of agree; though I find the opening tag requirement to be the crazy one :) (though I see the reasoning).

Disgusting is a bit strong though ;)


The same occurs in Drupal template files (which are powered by PHPtemplate, by default anyway). Including the closing tag with whitespace after causes a white screen of death which can be infuriating to debug - unless you know this.


The best way to handle this is not to even put a closing ?> unless you intend to have non-PHP in the file.




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: