Leaving Out the PHP Closing Tags

Typically, you will open and close PHP files like this:

<?php
// php code here
?>

Recently, I was introduced to another way. You leave them out!

<?php
// php code here
/* end of php file */

It seems a little weird at first leaving out the PHP closing tag. Feeling a little exposed and naked? It will take a little getting used to this way of closing out PHP. Somehow, it just doesn’t seem normal. This was done mainly to avoid the extra characters at the end of the file. You normally get a warning that headers were already sent or you cannot modify header information, if you have extra characters at the end of the PHP file.

I realized that you can only do this with certain files that end with PHP. For files that end with HTML or Javascript code, you will still need to close PHP further up the chain, otherwise the sky will fall on your head, and you don’t want that to happen. So, what do you think of think of leaving out the PHP closing tags?

4 thoughts on “Leaving Out the PHP Closing Tags

  1. I think it’s ok to use for small scripts or includes that are properly opened and closed in the file that included the snippet. Otherwise, it’s bad practice and I’d rather not risk overlooking some other code for the sake of 7 characters. Good to know though.

  2. I’ve been doing it a couple months now with small and large scripts. I haven’t had any issues yet. I must admit, it still looks real odd, but just that’s the way it is.

  3. Is this a mandatory change? It’s too weird for me… it’s one line “?>” why not just use it? I guess the end of the file chars, but how often do you use Header() redirects?

Comments are closed.