Why we don't close PHP tags…

bsoremsugar —  May 6, 2011 — 7 Comments

As a sign to all of the followers of the Sugar Developer Blog that we don’t just ignore the comments made to posts, I thought I would pull out a comment from my previous blog post to comment on. Here it is:

Hi John,
thanks for all. Can i ask something ? Why you don’t close de php tag ?
Is there any tip who can help for when use a close tag and where not ?

This is a pretty valid question, and probably one that is a bit confusing for those new to PHP. Let me explain…

As many of you know, PHP roots come from it being a language that was designed to be embedded within HTML markup, thus the need for tags in the first place to surround the PHP code that may be embedded within an HTML script. Nowadays, most PHP code written is kept separated from the presentation HTML markup, so files don’t necessarily need to jump back and forth between PHP and HTML. For these PHP only files you still do need an opening tag at the very beginning of the file, but you don’t need a closing tag since the EOF character is an implicit one effectively. Doing so has other benefits other than just saving a few extra characters, as pointed out in the Zend Framework Programmer’s Reference Guide:

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.

So leaving out the closing tag eliminates trailing whitespace, which can throw off AJAX responses and various other scripts in the application. You do of course need the closing tag whenever you are mixing HTML and PHP code still, but otherwise it’s generally considered best practice to leave it out, and we try to follow this practice in SugarCRM.

7 responses to Why we don't close PHP tags…

  1. 

    Hi John,
    it’s clear, it’s simply clarified
    Thanks many

    Cheers

  2. 

    nice news,so lucky for me to read it.thank you for sharing that.welcome to vist my blog website USA Soccer Jerseys

  3. 

    Thanks John, Today i learn something new through this blog.will be back soon please to keep it up..

  4. 

    Just what I was trying to learn. Thanks.

  5. 

    “leaving out the closing tag eliminates trailing whitespace, which can
    throw off AJAX responses and various other scripts in the application”..

    I m a newbie in php…Can anyone please enlighten me how can trailing whitespace throw off AJAX response…

    • 

      Typically AJAX responses just parse the returning payload as a JSON string. When you open up the possibility of addition newlines and other hidden characters, this can throw that parsing off.

Trackbacks and Pingbacks:

  1. why does translation files don't have ?> at the end when other files do? - January 2, 2013

    […] […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s