Summary of PHP echo shortcut tag <?=

PHP LogoYou may have read that using the short echo tag ( <?= ) should be avoided, especially for WordPress development. The convenience is really nice though, so here’s a quick summary of when you can safely use it in your code.

The problem with the short open tag

The echo shortcut syntax tag is not considered a “short tag” and is permanently enabled in PHP 5.4 and up. So if you’re writing code for PHP 5.4 you can use the echo tag without worry.

The problem is that the short open tag <? causes conflicts with xml. Unfortunately, in PHP 5.3 and earlier, the option to enable the echo shortcut tag was tied to the php.ini settings file with the short_open_tag flag. This meant that to get the benefit of the short echo tag ( <?= ), you had to also enable the short open tag ( <? ). The issues associated with the short open tag were much greater than the benefits from the short echo tag, so you’ll find many recommendations to turn short_open_tag off and not use it – thus throwing the baby out with the bathwater, as they say.

From my experience dealing with a large number of hosting environments with Cart66 development and support, it seems that most hosting environments, like shared hosts and places where WordPress might be installed, are configured to enable short tags in all versions of PHP. Since <?= is not regarded as a short tag, and apparently Rasmus Lerdorf himself made this very comment, it seems relatively safe to use <?= if you want to. For the widest possible compatibility, like you’d want for distributing a WordPress plugin, it’s still probably safer to just say 

<?php echo $something; ?>

How we handle the echo shortcut syntax

So… in the cart66 plugin, we will keep using the long version <?php echo because we want the code to run on as many possible configurations as we can and we have absolutely no control over the server configurations where the code is used.

In our other apps like CloudSwipe and Hurricane or any client projects where we control the hosting environment we use the <?= and enjoy the convenience of the concise syntax.

Since the echo shortcut syntax is permanently enabled starting in PHP 5.4, the more time that goes by the more reliable and universally acceptable the structure will become.

Leave a Reply

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