Categories
Posts

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]" *

9 replies on “Finding PHP Short Tags”

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.

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.

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]\|<?$" *

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 *