PHP Programming: Why You Should Omit ?> If It Occurs at the End of The File

Every single PHP documentation will tell us that in order to escape from HTML to PHP mode, we need to start with ``. What they neglect to say is that we don’t really need `?>` whenever it occurs at the end of the file.

In fact, it is better if we omit them.

We don’t need to put `?>` at the end of the file because when PHP encounters the end of the file, it will revert back into HTML mode. And therefore putting `?>` at the end of the file would be redundant.

It is more than that though. If we put `?>` at the end of the file, sometimes we need to try very hard that there’s nothing between `?>` and the end of the file. Or we might get the following error:

> Warning: Cannot modify header information – headers already sent by (output started at /foo/bar/blah.php:57) in /foo/bar/baz.php on line 57

Sounds familiar? This probably occurs more often with whitespaces before end of file than straight programming errors. Not really hard to fix, but annoying nonetheless. By simply removing the `?>` before the end of the file, we can avoid the problem altogether. We no longer need to hunt down those elusive invisible to the eye whitespaces. See?

Oh, and there’s more. Sometimes whitespaces will get there without you knowing, even after you ship the software. Consider these:

* Your user edits your software’s config.php, he/she unknowingly adds a line break or more at the end of the file.
* Text editors that ‘conveniently’ adds an ending line break when opening foreign text file format or a text file without terminating line break.
* FTP client that ‘conveniently’ adds a line break whenever it is transferring text files without terminating line break in ASCII mode.

It might be unbeliveable, but a simple task of removing `?>` that occur right before the end of every PHP file can and WILL reduce your support costs, however small it is.

That’s the reason why all my PHP program will not have `?>` when it ‘should’ occur at the end of the file.

By removing all `?>` that hugs the end of file, you will eliminate most if not all those ‘headers already sent’ support inquiries. However, if my experience is any indication, people will start ‘complaining’ that they get ‘incomplete’, ‘unfinished’ or otherwise ‘corrupted’ files. This is actually worse than those ‘headers already sent’ support inquiries. The solution is simple: just add a comment like `# end of file` or even `# this is really really really really the end of this file, there’s intentionally no PHP end marker here` at the end of every PHP file.

43 comments

  1. Baru tau…
    Dan gak pernah encounter such error. Sekarang nyoba juga gak ada error.
    Bisa kasi contoh sourcenya, gak, yang error “headers already sent”?

  2. does that include the file that used in include or require function?
    I haven’t tried yet, but I think it does.

    Yeah, answer my own question. I just need confirmation :P

  3. #13:

    does that include the file that used in include or require function?

    yes :) actually those ‘headers already sent’ errors can only occur when that happens inside included or required files.

  4. lah.. baru nyadar.. ternyat bisa to.. end of file.. malah nggak pakai tanda ?>

    Biasa-nya sih.. yang sering dengan masalah end of file itu.. gara2 kurangnya tanda kurawal tutup-nya.. (atau malah kebanyakan) :P

  5. I like those above reasons, but for me who has consumed Pascal and C in back oldies time, parenthesis or any other block open marker must be balanced.

    How if PHP uses one line marker like this (hyphotetical):

    # language PHP

    ;)

  6. hmmm layak di coba :) tapi kan itu kalo ending filenya kode PHP klo endingnya bukan PHP kan mesti di tutup juga tag phpnya? bukan gitu mas?

  7. Janjian nulisnya siang, jadinya tengah malam :). Hey, makasih ya. Tapi kayaknya sementara ini aku masih pakai cara lama deh. Pasti mula2 akan terasa gatal menulis atau membaca tanda . Sama seperti zaman BBS dulu, gatal kalau nulis :) tanpa pasangan ( di depannya.

  8. #19:

    I like those above reasons, but for me who has consumed Pascal and C in back oldies time, parenthesis or any other block open marker must be balanced.

    i think you need to change your coding paradigm :). for me denotes the start of php file, just like /usr/bin/perl for perl. because that way of thinking, there's no need to balance it. in fact, there should be no other instances of anywhere else. your code will be much cleaner, just like the good old perl codes :)

  9. #25:

    It will break my colors-full syntax highlight

    no, it won’t. at least it doesn’t break eclipse and vim syntax highliting here.

  10. Terima kasih memberitahukan cara menutupi kesalahan programasi. But for my part I prefer implementing good code than using that dirty tricks ;)

    Pasti trik ini berguna bagi programer php dari Indonesia yang ingin cepai selesai script tanpa terlalu perduliin kualitas script yang dikasih ke kliennya…

    Buat apa susah kalau bisa pakai jalan pintas begini ya? Tanya kenapa…

  11. From PHP manual:

    Note: Also note that if you are embedding PHP within XML or XHTML you will need to use the tags to remain compliant with standards.

    Btw, where is ur the comment preview pane?

  12. #33:

    Note: Also note that if you are embedding PHP within XML or XHTML you will need to use the `` tags to remain compliant with standards.

    that refers to `` tags as opposed to `` tags.

    comment preview is temporarily suspended after complaints from mac firefox users.

  13. #36 Selama ini (dua tahun lebih) saya tidak menemukan masalah dengan menghilangkan ?> di akhir file. Seorang teman juga sempat bertanya-tanya ketika saya mulai membiasakan untuk tidak menuliskannya (hai Ananda).

    Alasan saya juga benar-benar sama seperti yang di tulis Priyadi. Masalah syntax-highlighting, saya juga tidak menemukan masalah baik di quanta, vim, notepad++, ataupun dreamweaver.

  14. One of the Content Management Systems that I’ve been used since 3 years ago did this, omitting the ?> at the end of every .php files.

    And just to follow their coding styles/guidelines I did exactly the same when coding modules for that particular CMS whithout knowing the real reasons behind that. :D

    Thanks for the info, Mas Priyadi.

  15. very interesting readings!!

    tapi imo:
    yg harus diperbaiki bukanlah teknik menulis kita yg perlu menghilangkan ? dan > di akhir file. tapi interpreter php-nya yg harus bisa mengerti kalau karakter “kosong” jangan diproses.
    kenapa? karena kebiasaan tidak memakai ? > itu nanti akan seperti membuat sesuatu yg tidak ada penutupnya. seperti membuat “begin” tanpa “end”.
    it’s just not a good coding habit.

    but it is quite a good trick. where did you find it anyway?

  16. #41:

    kenapa? karena kebiasaan tidak memakai ? > itu nanti akan seperti membuat sesuatu yg tidak ada penutupnya. seperti membuat “begin” tanpa “end”.

    hehehe, paradigmanya harus diganti :). anggap saja PHP adalah bahasa pemrograman yang dimulai dengan sebagaimana perl adalah bahasa pemrograman yang dimulai dengan #!/usr/bin/perl :)

  17. Great tips! Juga untuk php files yang memiliki ekstensi bukan .php, misalnya .module, begitu disimpan tidak dalam format dos atau selain ANSI, misalnya malah memakai UTF8, juga menghasilkan error itu.

Leave a Reply to Joe Cancel reply

Your email address will not be published. Required fields are marked *