Using short versus long form of PHP open close tags

Posted in Articles

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

PHP code in HTML document is sectioned off by PHP opening and closing tags as shown here…

php long form tags

php long form tags

There is the question whether to use the long form as shown above versus using the short form as shown below…

php short form tags

php short form tags

In my case, both works and outputs “Hello World” to the browser.   That is because my server is set with short_open_tag set to “On” in the php.ini as seen when phpinfo() is invoked.   The short form of PHP open/close tags only works when that is the case, or if PHP was configured with the –enable-short-tags option.

But the long form works all the time.  Hence the long form is preferred.  PHP.net even says that short form is “discouraged”.

When using the short form, it can be ambiguous if you have inline-xml declaration somewhere in your code such as …

<?xml version="1.0"?>

As you can see, that xml declaration also starts with the same two characters as the PHP short tag — browser and yourself can both get confused.

The only advantage of using the short form is that the short form of echo can be used as in …

short form echo

short form echo

If the file is a pure PHP file, the closing PHP is preferred to be omitted.