Re: PHP code only allowed in XHTML 5?

On 22 Jul 2009, at 09:47, Daniel Glazman wrote:

> Toby A Inkster wrote:
>
>> <p class="<?php echo 'foo';?>">
>> Invalid in both XHTML and HTML, but perfectly legal PHP. PHP's  
>> start and end markers are not real XML processing instructions.  
>> They just look a bit like them.
>
> They are real processing instructions and the current PHP start marker
> <?php was not the one early versions of PHP used. It was changed to
> match SGML/XML PIs to allow editability in markup editors and wysiwyg
> editors.
>
> Breaking that compatibility is, IMHO, a serious error.

They are not. They are just roughly similar and compatible only in  
most basic cases.

The example given by Toby doesn't create processing instruction in XML/ 
SGML (even if < was allowed in attributes, it would be equivalent to  
"&lt;?php"), and yet is interpreted by PHP.

You can't even reliably parse such XML/SGML-like documents without  
understanding of PHP syntax:

<?php echo ' ?>'; ?>

is seen as one processing instruction by PHP, but in SGML/XML that is  
interpreted as processing instruction followed by "'; ?&gt;".

Similarly with <!-- <?php " this is not PI in XML, and you can't even  
find end of the comment "; if ($x-->1)  ?> -->.

There are many examples which will cause false positives:

<?php if ($items) echo '<ul>'; foreach($items as $i) { ?><li>...</li><? 
php } if ($items) echo '</ul>' ?>

and mistakes that will not be found:

<?php echo '</div>'; ?>


Validation of PHP files cannot work reliably, it never did, and it  
would be disservice to authors to pretend otherwise.

If you want to ensure that code PHP generates is valid/well-formed, I  
suggest using templating engine for PHP that doesn't break XML syntax,  
for example PHPTAL or OPT 2.0.

-- 
regards, Kornel

Received on Wednesday, 22 July 2009 13:03:50 UTC