PHP Short Code – How to Change to PHP Long Code Syntax

The other day I ran into an interesting problem.  I had to convert an entire code base from using PHP Short Tag syntax, to using their Full Tag syntax.  This post is created to share how I did it in case others need to do the same.

What are PHP Short Tags a.k.a short_open_tag?

PHP Short Tags are a setting within PHP that allow you write less code when you open you PHP code blocks.  By default this setting is enabled when PHP is shipped.  The setting within php.ini is a boolean and named as follows:


PHP provides this explanation:

; Allow the <? tag.  Otherwise, only <?php and <script> tags are recognized. NOTE: Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags.

short_open_tag allows you to open your php code blocks using simply <? instead of the full <?php syntax.  This might seem like a trivial change – but habits are hard to change if you have coded with <? your whole life.

So the general rule applies.  If your php code will only stay on your servers, then use whichever setting you prefer.  If your code will be shared or redistributed, it is better to turn that setting off an use long code syntax.

How to change short_open_tag for an entire codebase.

Changing from short_open_tag enabled to short_open_tag disabled can be a rather daunting task.  Thankfully a trick in REGEX exists to make it a little easier.

You first step is to make an exact duplicate of your code base.  When you are doing global search and replaces you need to ability to start over in case an error occurs.

Now let’s have a look at your IDE.  Hopefully your search tool allows you the ability to use Regular Expressions in your searching.  If it does not then this search will not work as you need to be able to send through an important test command.  The command is the ability to test for matches when no other strings exist after the search string.

Look at the image below.


In this image we are using the following search string in Regular Expression:


This search string is allowing us to find all of the instances where <? is found.  Let’s assume the following examples

  1. <?
  2. <?php
  3. <? echo “My Text” ?>
  4. <?=($myText)?>
  5. My Text <? echo “Some Other Text”; ?>
  6. My Text <?=(“Some Other Text”)?>

Here are the results using the above search expression

  1. Found
  2. Not Found (because it has characters after the ? symbol
  3. Not Found (text follows.. can handle with non regex search/replace)
  4. Not Found (which is fine – this syntax is not governed by short_open_tags since PHP 5.4)
  5. Not Found (text follows.. can handle with non regex search/replace)
  6. Not Found (but again – we don’t want to replace this one)

Look at examples 3 – 6.  This is why the regular expression is so important.  It allows us to find the <? statements where no other code follows it.  Now examples 3 and 5 we still need to fix.  Well these ones are easy to fix using standard search and replace (without regex).

Search for “<? ” and replace with “<?php ”

Notice the space character in the search string?  That will allow you to find sample 3 and 5 above.

I hope this helps.  For more info on short_open_tag you can view the PHP Doc here

Posted in , , and tagged , .