Should You Close Your PHP Code Tags?

    Craig Buckler

    Even those with a modest grasp of PHP know that code must be enclosed within special <?php and ?> tags.

    note: Alternative PHP tags

    You may also be aware that PHP code can be delimited with the lesser-used <script language="php"> and </script> tags.If short_open_tag is enabled within php.ini, you can use <? and ?> although they should be avoided if you’re embedding code within XHTML or XML.Finally, you can use the ASP-style <% and %> tags if asp_tags is set within php.ini.

    However, if your file contains just PHP — and no escaped HTML code — the closing ?> tag is entirely optional. Many developers argue that unnecessary code should be removed but there’s another reason you could consider scrapping the closing tag.Assume we have a PHP function library named library.php:

    <?php// library functionsfunction DoStuff() {	// code}?> 

    The library’s included inside our main entry file, index.php:

    <?phpinclude('library.php');// write a headerheader('X-Demo: Example');// set cookiesetcookie('TestCookie', 'Example');?><p>End of index.php file.</p>

    Unfortunately, 2 warnings with the same message appear when this page is loaded:

    Warning: Cannot modify header information - headers already sent

    Or worse, if you could be running in a live environment where warnings have been disabled and no message appears. In either case, neither the header or the cookie is set and that could cause critical application problems.What’s causing the error? You can’t see it, but there’s a space character following the closing ?> in the library.php file. When it’s included at the top of index.php, that space is sent as page content to the browser — along with all the necessary HTTP headers. Once the first block of content has been sent, it’s not possible to set additional headers or cookies.

    note: PHP output buffering

    Modern versions of PHP set the output_buffering flag in php.ini. This buffers your HTML output and sends it when your PHP code has been processed or once the buffer reaches a limit (e.g. 4,096 bytes). You can also use PHP’s ob_start() and ob_end_flush() to implement your own buffering functionality.If output buffering is enabled, you can set HTTP headers and cookies after outputting HTML because returned code is not sent to the browser immediately.Note that older versions of PHP and some ISPs do not enable output buffering — it hits server performance and requires more memory. Even if you’re certain buffering is always enabled, it’s good practice to set HTTP headers and cookies before sending page content.

    Your PHP application could include dozens of library or class files. As you can imagine, it can be difficult to hunt down additional spaces, carriage returns, or any other characters following a closing ?>. Fortunately, there’s an easy fix. If you omit the closing ?> in all your PHP-only code files, the error simply can’t occur — the parser will ignore whitespace.It’s a solution, but would you use it? It makes me feel a little dirty…Do you already omit the closing ?> tag? Would you adopt the practice? Or does it just feel wrong?