Reminder: Don’t Use the Short PHP Open Tag

Jetpack 2.0.1 was released last night, immediately followed by 2.0.2, which fixed a fatal error on some hosts, caused by a short PHP open tag. So here’s a reminder: never use the short form of the PHP opening tag:

<? _doing_it_wrong(); ?>

Always use the long form:

<?php _doing_it_right(); ?>

If your grep can do Perl regular expressions, you can search your entire codebase with a negative lookahead like this:

grep --include=*.php -rP '<\?(?!php|xml)' *

Joseph Scott has a simpler example that works without the -P flag. If you’re building WordPress themes, the Theme Check plugin will scan your theme for short tags automatically. Plugin Check will do the same thing for your plugins.

Stay safe.

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.

11 comments

  • You can also disable it globally in the php.ini.

    short_open_tag = Off
    

    That’s what I do for any dev environment (to reproduce the lowest common denominator).

    • That’s a great tip Brent, thanks for your comment! Having your IDE configured to not accept short tags is also a good choice, though I work in way too many different environments to remember to do that everywhere. I think the WordPress plugins repo should just have a pre-commit check and fail if short tags are used.

    • A pre-commit check on wp.org would be great. It would prevent any problems for newer programmers who aren’t even aware of short open tag issues (as well as those veterans who just missed one or two).

  • Also, omitting the closing tag at the end of the file is a good way to get rid off the whitespace-related issues.

    We, at some point, all spent hours to find the reason of the “Headers already sent” error and bashed our heads against our desks when we realised it was caused by a new line after the PHP closing tag :)

  • I find it odd that PHP 5.4 introduces the short echo syntax after all the fuzz around the short tags before that. That’s another thing we should be aware of until they decide to drop it in, let’s say, PHP 5.7.

    • I agree, your concerns are valid, though some developers find it quite attractive, and as opposed to the short open tag, there should be no conflicts with XML. In any case, just to be on the safe side, I recommend to use the long form, no matter what. Thanks for your comment Mario :)

    • I’m all for removing support for php short open tags all together.

      Short echo tags are annoying. I think this was done for the point of being lazy for web designers, or to “make it easier” on web designer, designer, not the developer. From my perspective, with the many languages I have developed in, the equals sign is for assigning a value to something.

      It’s one of those things complicating the language by giving options that shouldn’t really be there, when they should have been fixing things like needle and haystack orders and naming conventions.