X

Customer Login

Naughty boy ! What is this short_open_tag ?

Posted by antoine on 2009-05-28 in php, symfony

PHP coding standards strongly recommend to use full php open tags.

We recently updated an old PHP project running on a partner company proprietary framework that made an heavy use of short open tags. Whether it was <? or <?=, basically almost any call to php was done using the short ones.

We decided to take care of this issue and replace all the short open tags by full ones. The best solution would be to use a processor that knows the structure of the language in order to validate each replacement. However after googling the problem, we couldn't find that kind of solution easily, and we didn't have much time to spend on it.

What we found is that most of the solutions provided simple text replacements through scripts (wether it is PHP, Shell, Python...) even if these replacements could be done using 1 or 2 command lines.

We finally decided to address the problem by writing our own Linux command lines using find, sed and xargs. The first line replaces the open only tags (<?), the second replaces the open and echo tags (<?=) :

find . | grep "\.php$" |xargs sed -i 's/\(<?\)\([^a-zA-Z=]\|$\)/<?php \2/g'
find . | grep "\.php$" |xargs sed -i 's/\(<?=\)/<?php echo /g'

Important : Even if these commands worked "out of the box" for us, we did check every line replaced to be sure nothing was broken and tested our application. We strongly advise you to do the same. If you find any issue using these commands, please let us know so that we can fix them and update our post.

One comment so far

Posted by Daniel on Friday May 29th, 2009 23:58
Hey Antoine, that's a good way, too, thanks for posting those patterns. I just thought I'd share that I once worked with a server that wouldn't recognize the short tag at all. A quick workaround was to adjust the configuration for that particular site. I've posted the code a while back over here http://www.codemassacre.com/2008/01/20/php-short-opening-tag/ in case anybody ever needs the "quick" fix.

Leave a reply

Captcha picturereload