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.

5 comments 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.
Posted by Ryan on Friday January 14th, 2011 00:58
I did test the application and everything was good. Thanks for the information, it helped me out.
Posted by Roger on Monday January 31st, 2011 16:35
Me too Ryan. At first I thought something was wrong, but in the end everything was good. There are only so many blog owners who will give out good advise, so when it comes along I have to utilize it. Thanks!
Posted by games search on Friday June 24th, 2011 10:47
Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how could we communicate?
Posted by REPLICA WTCHES on Tuesday October 25th, 2011 08:16
You have to remember these common sense tips before buying designer watches

Leave a reply

Captcha picturereload