News Archive
PhpRiot Newsletter
Your Email Address:

More information

Should You Close Your PHP Code Tags?

Note: This article was originally published at Planet PHP on 18 September 2010.
Planet PHP

Even those with a modest grasp of PHP know that code must be enclosed within special tags.

note: Alternative PHP tags

You may also be aware that PHP code can be delimited with the lesser-used and tags.

If short_open_tag is enabled within php.ini, you can use although they should be avoided if you're embedding code within Xhtml or XML.

Finally, you can use the ASP-style 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:

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

End of index.php file.

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 dirtya

Do you already omit the closing ? tag? Would you adopt the practice? Or does it just feel wrong?

Related Posts

  1. How to Code Like a Zen Master
  2. My Favorite tags
  3. Is Using Lots of div Tags Really That Bad?