4 ms·
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 a
by raffi 17y ago
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-modify-header-information-headers-already-sent http://stackoverflow.com/questions/1793482/php-error-cannot-...
The work around: omit the closing ?> tag from included files.
- bendtheblock 17y agoThe 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.
- randallsquared 17y agoThe best way to handle this is not to even put a closing ?> unless you intend to have non-PHP in the file.
- dutchflyboy 17y agoThat'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.
- andrewvc 17y agoOuch, 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.
- ErrantX 17y agoMost PHP dev's omit the closing tag anyway for a variety of reasons (mostly because, well, it's not needed)
- raffi 17y agoTo 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.