ThemePartner, Professional Joomla! Business Templates

koen

The PHP Closing Tag Explained

November 5th, 2010 at 12:47 PM CET in Technical

Back in the old days of PHP (including business logic), and HTML were often mixed in source files. The opening and closing tag of PHP made this intermixing of code and markup possible. These days this mixing is far less common and even more open source projects are dropping the PHP closing tag.

PHP closing tag

See the (awful) code below for an example of mixed HTML and PHP code:

<table>
 <?php
 $items = array('Joomla', 'Wordpress', 'Drupal');
 foreach($items as $item) { 
 ?>
 <tr>
 <td>
 <?php echo htmlspecialchars($item); ?>
 </td>
 </tr>
 <?php
 }
 ?>
</table>

Headers already sent

A common problem with the closing tag is that when hard returns (or HTML code, CSS markup etc.) are following it, you can get the following error message: "Warning: Cannot add header information – headers already sent by (output started at index.php:3) in index.php on line 1". This is illustrated by the example below:

<?php
// cool stuff here
?>
[empty line]
[empty line]
[end of file]

The closing tag is often not required

Nowadays PHP code (including business logic) and HTML are often separated in different files. A popular design pattern used to separate business logic from markup is called 'MVC' (Model View Controller) which Joomla! is using as well. This allows you to separate specific parts of your site, like the interface and business logic. More information about MVC can be found here.

When PHP code and HTML are separated you won't need a closing tag in your PHP files anymore. Since PHP doesn't force you to use it, the following example works just fine:

<?php
class Test {
 public function doSomething() {
 // code here
 }
}
[end of file]

ClosedPeople that are used to adding a closing tag to every file may find this strange, but this is perfectly valid code. To illustrate that statement: Zend Framework (a highly popular PHP framework) forces omitting the closing tag in their coding guidelines: "For files that contain only PHP code, the closing tag ("?>") is never permitted. It is not required by PHP, and omitting it prevents the accidental injection of trailing white space into the response."

Popular open source projects

Below a list of open source projects that are omitting the closing tag:

And open source projects that are using the closing tag:

Should I use a closing tag?

Personally I think it is better to omit them. Your code is perfectly valid without them and it prevents accidental errors, so in my opinion there isn't any reason to use them (assuming you are separating your business logic from your content of course).

Closed image credits: Tim Lucas

Follow us on Twitter

User Comments (2)

Add comment

mario

November 5th, 2010 at 4:25 PM CET
Omitting the closing tag is a good recommendation to PHP newbies. PHP help forums are littered with questions around "headers already sent" and alike. Giving this tip out to newcomers at least postpones that.

But somewhen they'll stumble across an edited PHP library or class. If someone is using a substandard text editor, extra whitespace will inevitably appear after the php closing tag. Windows users will additionally add up \r\n instead of a simple newline. And the PHP tokenizer only eats up the first newline (not the carriage return) after the closing tag.

So, if you are new to PHP, use Windows, and neither a CVS/SVN server which automatically cleans up that syntax attribute, it's really best to avoid.

mario

November 5th, 2010 at 4:27 PM CET
Nitpick: It's PMVC. The real MVC pattern is only applicable to GUI apps.

Add a comment

*
*
*

*

 

Buy 2 Templates, Get Your Third Free!

Buy 2 Templates, Get Your Third Free!

By popular demand we've introduced a way to purchase multiple templates, at a discounted rate! If you add any three templates to your shopping cart, you'll only have to pay for two!

Blog categories

Conferences

Upcoming conferences

Past conferences

Reports on past conferences