PHP Closing tag

There is much talk about whether to have a PHP closing tag "?>" at the end of a file with a ".php" suffix or not.

Here is the potential problem that having the tag can cause. In the event that there is anything following the tag in a file that is included the lines, empty or not, that follow the tag will be sent to the browser immediately therefore causing headers to be sent and preventing any operation that needs to be performed before the headers are sent from executing correctly.

Of course one solution is to say well you should be more careful and ensure that the end of your file is cleanly marked by a closing PHP tag where appropriate. This is how it always used to be. If as a programmer you did not correctly use the syntax then your program failed. You quickly learned to follow the rules.

However today we have to bend the rules to accommodate the ones who cannot be bothered to follow them. For example Zend the creators of PHP have now changed the rules on their IDEs to enforce no closing tag in PHP only files. What is this teaching the programming community ? probably that if enough of them have sloppy coding methods that eventually the system will change to accommodate them.

Anyway I digress.

If you are using Dreamweaver prior to CS5.5 then you will find that a missing PHP closing tag messes up the colour coding which is annoying because you have a big splash of yellow highlighting at the front of your file.

Here is a solution, just use it with caution and it doesn't insert PHP closing tags, but what it does do is remove all the blank lines in all the files. Which is why you need to be careful about which files you apply this to.

Do not just blindly run it against all the files for your site, for one thing it would be a waste of time against any non PHP files and for another some files may well need blank lines for formatting (this would not apply to HTML or PHP files).

Dreamweaver's Find/Replace function can use Regular Expressions here is one that removes blank lines from a file :

In the find box type (without the quotes) "\n\s*\n" and in the replace box make sure that it is empty then hold the Shift Key whilst pressing Enter, this place a CR/LF in the replace box and check the Use Regular Expressions checkbox. When applied to a single file it will remove the blank lines.

If you have your whole site on local storage you can click on the root of the site and then press Ctrl-F to bring up the Find/Replace dialog box, now when you apply the find/replace regular expression and choose replace all it will run against all the files in the folder.

Just be sure that the files that you are cleaning need to be cleaned and that removing the extra lines is not going to cause more grief than it solves.

How does this help ? After running this solution upload all the cleaned files to your hosting and you will find that the "headers have already been sent" issues relating to extra lines following a closing PHP tag will have gone away.

What you do about having closing tags in files that you create is up to you, at least now yours and anyone else's problem causing blank lines will have been removed.