Using short open tags, such as <? on your PHP code is not so clever. Other than the debate going on whether short open tags won’t be available on PHP6 or not, it is possible that your shared hosting has disabled it for some reason, or you might have to migrate your code sometime somewhere it is not supported any longer. For the sake of clean coding, always use clean tags. Other than obeying the standards for technical reasons, it is also essential for developing clean, easy-to-read codes for yourself, other people reading your code or even the IDE/Framework you’re using!

If somehow, this article is the first place you’ve realized you should stop using short open tags, it’s never too late! Below is a find / sed line to convert your php files from short open tags to “normal” ones. This line works pretty good but it’s always a good idea to backup your scripts before doing such massive automated injection.

find . -iname '*.php' -type f -print0 |xargs -0 sed -i -e 's/<? /<?php /g' -e 's/<?\/\//<?php \/\//g' -e 's/<?\/\*/<?php \/\*/g' -e 's/<?\=/<?php echo/g'

Don’t forget that this line will look for *.php files under the current folder due to the dot (.) which is the first argument of the find command.
It will apply these replaces:

From To
<? <?php
<?// <?php //
<?/* <?php /*
<?= <?php echo
One Response to PHP : Convert/Replace Short Open Tags
  1. […] POSIX… si vous y arrivez laissez moi la solution en commentaire. D'ailleurs je déconseille (1, 2), ceux qui essayent de penser à tous les cas possibles, c'est le meilleur moyen d'en […]


Leave a Reply

Your email address will not be published. Required fields are marked *