Kim Madsen wrote:
Mattias Thorslund wrote on 09/01/2010 02:26:
A neat thing with pairing every <?php with a ?> when mixed in HTML is that these are valid XML processing instructions. If your HTML satisfies XML well-formedness, your PHP document will also be valid XML. Not that I've ever had any need to process my layout templates as XML but anyway.
I don't see your argument. PHP generates HTML or XML files. When you're aware of the XML tag of course you make sure the XML file generated is valid. Why would you ever add PHP code to a HTML file (other than for documentation, examples etc.)?

It's not much of an argument, just an observation. And the conclusions are disappointing anyway.

Certainly, if your script is designed to create XML, you'll make sure that output is valid when executed by PHP. But it is interesting to note that your PHP script file itself might qualify as valid XML under certain circumstances, in which case you could process it with an XML processor. This would let you modify the XML and still keep the PHP part of the file intact.

I haven't seen a live example that might benefit from this, but I still find it interesting.

The conditions that would need to be met, are that the content outside the <? and ?> processing instructions must be well-formed XML, and there are places where the processing instructions aren't expected in XML.

This is well-formed XML (and valid PHP):

<?xml version="1.0"?>
<?php $var = 'dynamic'; ?>
<root attr="static">
   <?php echo "$var content from PHP\n"; ?>
</root>
<?php //processing instructions can also be located after the XML content ?>

This is not well-formed XML (processing instruction within a tag). Note however that the output from PHP would be valid XML:

<?xml version="1.0"?>
<?php $var = 'dynamic'; ?>
<root <?php echo "attr=\"$var\""; ?>>
   <?php echo "$var content from PHP\n"; ?>
</root>

Neither is this (processing instruction within an attribute):

<?xml version="1.0"?>
<?php $var = 'dynamic'; ?>
<root attr="<?php echo $var; ?>">
   <?php echo "$var content from PHP\n"; ?>
</root>

And neither is this (missing opening tag for the "root" element):

<?xml version="1.0"?>
<?php $var = 'dynamic'; ?>
<?php echo "<root attr=\"$var\">\n"; ?>
   <?php echo "$var content from PHP\n"; ?>
</root>

A simple way to test this would be to use this:
$sxe = new SimpleXMLElement('the_file.php', null, true);
echo $sxe->asXML()."\n";

The inability to insert dynamic content into attributes without breaking the XML (ideas anyone?) does limit the usefulness of this technique, so I guess it will just remain a curiosity.

Cheers,

Mattias

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to