When developing PHP scripts, I often find people logging ‘bugs’ about missing closing PHP tags. i.e. ?>
I want to make something clear, this is not a requirement unless you plan to stop PHP and output HTML. The following is a 100% valid PHP file:
<?php echo "Hello, World!";
Why do it? The best reason of all is to prevent unintended whitespace. If you have a closing PHP tag and plan to use the header() function at all (or any function that outputs a header), you need to be careful you don’t have newlines or spaces after your closing PHP tag (headers need to be sent before ANY output, including spaces and newlines). The best solution is to kill the closing PHP tag, so that nothing is output without you intending it.
Update: Another point for no closing PHP tags is security. I’ve seen numerous sites that have spam links injected into them. Usually the spammer gets access to the server through a vulnerability somewhere and injects all index.php files with HTML at the end. If you do not have a closing PHP tag, the HTML will generate a parse error and never be output. The spammer loses, and you find out straight away that something is wrong on your site!
No related posts.









Leave Your Response