Skip to navigation

malevolent design weblog

This blog is now defunct, but you can find more stuff over at my personal site

In Defence of PHP Short Tags

On most servers, PHP will happily let you begin a block of code with either <?php or just <? , but the latter is widely frowned upon. Spend a few minutes Googling and you could be forgiven for getting the impression short tags are thoroughly evil and will eat your code/children while you sleep.

Not everyone has them enabled, so you need to avoid short tags for maximum compatibility, but why disable them in the first place? Aside from circular reasoning (disable them because they’re bad; they’re bad because some have them disabled), all I can see are misleading asides (scroll down to ‘Note:’) and grumbles about ‘incompatibility’ with XML/XHTML. Why would you expect to be able to parse unmodified XML markup as PHP code without potential problems, or validate PHP code as XML markup? It’s just not reasonable or necessary. Oh, and you only need to add 4 characters to an XML declaration/processing instruction within a PHP file to avoid a clash, e.g.:

<<??>?xml version="1.0" encoding="UTF-8"?>

But instead of easily working around that one minor issue, we’re supposed to add clutter throughout all of our code, turning

<? $something = 5; ?>
<p>Something is <?=$something?></p>


<?php $something = 5; ?>
<p>Something is <?php echo $something; ?></p>

The conventional wisdom is wrong and harms readability; if anything, short tags should be the standard.

(Yeah, I know it’s a non-issue if you use something like Smarty, but with short tags enabled a templating system that uses PHP itself can be just as tidy and a lot faster.)


I'm with you on this one. I have always used short tags, but was only half-aware of the "problems" with using them. All I knew was that these "problems" never amounted to anything in my experience and that I was getting far more concise and readable code. Thanks for doing the next step of finding out that the dangers really are bogus.

— Greg, 26th Oct, 3:56pm

couldn't agree more.

tyler j wilkinson, 29th Nov, 3:44pm

We are having this argument at work right now. Readability goes to hell in a hand basket without short codes.

Chris, 3rd Dec, 2:38pm

Comments are now closed for this entry.