I recently swatted a bug that admittedly had me
bamfoozled for quite some time. The problem was that PHP code ... code which had been running flawlessly for years ... suddenly stopped producing usable images. Everything that it produced was corrupt.
Here's an excerpt of the
broken code:
Code:
<?php
// blah blah blah ...
?>
<?php
// blah blah blah ...
// (do you see the bug yet?)
// blah blah blah ...
header("Content-type: image/jpeg");
imagejpeg();
?>
Do you see it yet? I'll wait ...
"Boo bee boo bee, boo doo doo ..." (ding!) Time's up!
This code is
intended to produce
only the output of the
imagejpeg() function, with an HTTP header that specifies that the data is an image. So far, so good.
But... here's the bug: There is an
extra newline ($0A) character that is added by the
blank line between the two
<?php tags at the start of the file. The browser sees this data,
including the unwanted newline byte, as a (corrupt) image.
If this blank line is removed, the newline disappears and the code works correctly. Like this:
Code:
<?php
// blah blah blah ...
?>
<?php
// blah blah blah ...
(You may now make your donations, in my name please, to diePHPdiediedie.org ...)