Better way to write php code – part 1

A few tips on writing php code that can save a php developer a lot of future troubles and “huffs and puffs” occurring due to different php compiler settings, changing of hosting environment etc. Here is first one.

Short Tags

It seems more easier writing “Short PHP tags” and it becomes more prompting specially when facing tight deadlines. And why not, writing <?=$fullname?> in place of <?php echo $fullname; ?> can save you at least 9 characters in hand. But following this, it can get you into trouble, if PHP Compiler’s “short open tag” support is “turned off” and you don’t have control over server’s settings. If “short open tag” is “Off” your code <?=$fullname?> is going to print nothing but an empty space.

Also, quite a few times I have learned that “short open tag” support is likely to get deprecated in future php releases, and if that happens, your entire application becomes inevitable to be performed a “search and replace” for the occurrences of “short open tags” which is not that simple what it sounds like.

To avoid this problem, use <?php echo $fullname; ?> in place of <?=$fullname?>. So, what is the wisest work around which could save you writing those 9 characters every time you write an echo statement? Well, in fact you can save a lot more! This is how:

Create a new function in a common include file, perhaps in most commonly used “includes/functions.php” as below:

function e($content)  {
echo $content;
//print $content;
/*or use print, whatever you like most
but echo seems a bit faster*/

and call this function wherever you want to print the content. For example,


How many characters does it save exactly ;)

(Keep an eye on the next post of this series)

Leave a Reply

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