Recently I’ve been checking out PHP sources of a friend’s web application. I’ve decided to use Gedit, as we needed a quick solution and eclipse was not in place (yet). We’ve managed to setup file permissions and database pretty fast and finally – we got the application working, but no script-generated images were showing.
Interesting enough – downloaded image files appeared to have normal sizes (and different ones, too), but browser couldn’t display them. Neither could any of imaging software available.
Opening image file in a text editor revealed an empty line at the beginning which when removed made file usable! So, we definitely have an accidental newline that slipped outside block of PHP code.
Fortunately, it was only a single config file that I’ve edited and since application works elsewhere, I easily spotted the newline character at the end of it. After deleting the newline and hitting ctrl-s I quickly alt-tabbed to confirm it works now. Still the same.
Back to Gedit – newline was still there. I knew I’ve only changed vital settings and never touched the end of the file at the first place, so it was really annoying. Quick search on google for “gedit adds newline” reveals that I’m not the only one caught in this:
I’m not a c/c++ developer, i’m php developer. but I use gedit for my php developing. Gedit auto adds a new line at the end of file, which will cause a problem when the program runs. I hope this action can be configurable. (from https://bugs.launchpad.net/ubuntu/+source/gedit/+bug/379367)
It’s interesting that this is actually considered a “feature”…
Now, to the solution. PHP could have problems with newline only if you end file with:
Actually, any trailing whitespace could break the script purpose. A good practice is to omit “?>” from PHP scripts at the end of the file. This will not result in a syntax error (that we’d all expect) and could lead to accidental whitespace (or automatic newline) problems that are really hard to find when you have many files.
Still, I’m convinced that really ANY automatic modification to an edited file must be configurable.
Being unable to turn this off is like being unable to turn off auto-correct spelling errors in a word processor software. No, this is even worse – you can’t revert auto-corrected changes because file is modified right before saving.