PHP Short open tags vs long open tags

The PHP short open tag looks attractive in the beginning but there are a few good reasons not to use short open tags. The benefit of short open tags is that you can write slightly less code, this is most beneficial when inserting pieces of PHP code into HTML where your code might look something like this:

PHP Code <html>

<head>

<title><?=$title?></title>

...

Without short open tags, the above code would have to be:

PHP Code <html>

<head>

<title><?php echo $title?></title>

...

The downside

First and foremost, short open tags must be enabled in the servers php.ini for them to work. The option short_open_tag must be set to 1.

If you try to use short open tags on a server that has them disabled in the php.ini then the PHP interpreter will ignore your PHP code and simply display the raw code instead of executing it.

So if you happen to be deploying your web application on a server that has short open tags disabled, and you don't have permission to modify the php.ini then you're going to have to clutter your code up with calls to ini_set() to temporarily enable short open tags.

Who's the winner?

Best practice is to never use PHP short open tags because you will never be presented with the above scenario. Personally I think code is easier to read anyway with long open tags. The other small benefit of long open tags is that the code is quickly identifiable by someone that does not know what PHP syntax looks like.

Comments

There are 0 responses. Why not add a comment? No registration required.

Leave a Comment

Enter Code

Refresh code

Menu

Firefox Addons