Finding PHP Short Tags

There are a number of ways to escape between PHP and output (usually HTML) mode. Personally I recommend sticking with the traditional <?php style. One method that can cause problems is the so called ‘short tag’ style – <? and I recommend avoiding it.

So how do you know if there are PHP short tags being used some where in your code? I use grep to search for them: grep -rn "<?[^p]" *

8 Comments

  1. I use <?php tags, but what is problem with shorttags? I never found explanation.

  2. The <?php tags are the only ones that you can be sure will work across all PHP installs. That’s the big one for me.

  3. Thanks for the tip, Joseph. Very useful—I’ve added it to my arsenal of theme testing checks.

  4. No problem, happy to hear it helped.

  5. I like to use short tags for personal and client work, when I know I have full control of the hosting environment. I’ll use full php tags for public and open source code.

    It kinda sucks that short tags aren’t enabled by default. is so much nicer than . Less typing, and readability is better.

  6. The situation where you just need to do a simple echo would be nicer, but could potentially be accomplished with <?php= $some_var ?> – hopefully PHP core will support that syntax one day.

  7. The only problem I find with your regex when I try it, is that it doesn’t find if the line ends in “<?" (which happens if that's the whole line, a common occurrence). It's not counting the newline as "not p". You'll want something more like this:

    grep -rn "<?[^p]\|<?$" *

  8. In current versions of PHP, short open tags are explicitly allowed for the simple echo case: <?=$something?>, but not in other cases. To find disallowed short open tags (i.e., short tags other than the above), use this: grep -rnl "<?[^p=]\|<?$" *. This borrows from Erik Olson’s comment above to find short open tags at the end of a line, but doesn’t find simple echo short tags, which are permitted.

    TRiG.

Leave a Reply

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

© 2018 Joseph Scott

Theme by Anders NorénUp ↑