PHP short tags/xml patch (AKA long live short tags!)

For years I’ve been seeing people try to get rid of PHP ‘short tags’ – <? and <?= to be precise – and enforce “long tags” only. This means code like this:

&lt;?=$foobar;?>

would have to be written like this:
&lt;?php echo $foobar;?>

Not tying directly, but this seems to fall in line with people who like Smarty and other ‘templating languages’ instead of just using PHP itself for the ‘templating’ part.

{$foobar}

or

&lt;?=$foobar;?>

are both about the same, but the first one has extra parsing overhead involved (yes, even just that one time, via Smarty, there’s overhead).

The only ‘valid’ reason I’ve ever head for disabling the ‘short tag’ approach is that PHP parsing a document with XML in it would throw an error, because XML uses <?xml as a declaration that this is an XML document. Nevermind that PHP supports <% ASP-style tags as well – we’ll apparently never get widespread adoption of those (though for some reason Ruby supports it, at least via Rails). To purists, the only thing we can do in PHP is all disable short tag support, type more, and use Smarty.

Well, last year I made a quick patch to prove that the XML tag conflict was something that could be taken care of at the PHP parser level. It worked, but I lost that patch. So, I put it together again and would like some feedback. It seems to work OK on my end, and I’d like to see if this is something that we could get some traction behind.

Using PHP 5.1.1 source (this should work in 4 as well, which is what I used last year) go in to the Zend directory, and edit zend_language_scanner.c

Change line 4622 from

if (CG(short_tags) || yyleng>2) { /* yyleng>2 means it's not &lt;? but &lt;script> */
to
if (CG(short_tags) && yyleng>2 && (yytext[2]!='x' && yytext[3]!='m' && yytext[4]!='l' && yytext[5]!=' ')) { /* yyleng>2 means it's not &lt;? but &lt;script> */

I might be missing something changing the || to &&, but my initial tests indicate not.

For those of you looking at PHP, this should be the same, although maybe not line 4622. It’s in the

case 112:
YY_RULE_SETUP
{

area, if that helps.

This should make a PHP file like the following possible:

foo.php
&lt;?if($_GET['xml']) { ?>
&lt;?xml version='1.0'>
&lt;foo>
&lt;bar val='1'>baz&lt;/bar>
&lt;/foo>
&lt;? } ?>

Feedback?

——————————

UPDATE:

This thread
seems to indicate there might be other problems when trying to output other types of ‘valid’ XML tags in PHP – <?foo might be seen as a valid XML Processing Instruction, for example. Not sure how much of an issue this would really be.


I'm currently working on a book for web freelancers, covering everything you need to know to get started or just get better. Want to stay updated? Sign up for my mailing list to get updates when the book is ready to be released!

Web Developer Freelancing Handbook

Share and Enjoy:
  • del.icio.us
  • DZone
  • Facebook
  • Reddit
  • StumbleUpon
  • Digg
  • Simpy
  • Technorati

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">



0.16984987258911