Search code examples
phpxmlcolor-coding

PHP XML declaration causing compiler color-coding error


Hello i'm currently using Microsoft Webmatrix and whenever i do an XML declaration my compiler confuses it with a tag and caused all the rest of my PHP code to be all black and not color coded, also all my HTML to be beige colored as if it was text. This is becoming a big problem because my webpages are becoming very bulky and the readability of it is an absolute nightmare.

example of xml declaration:

<?php
    $ThisvariableisNOTblack = "good";

    $xml = <<<XML
    *xml content*
    XML;

    $thisvariableisnowblack = "everything is now black below XML";
?>

Is there an alternative to this style of XML declaration?


Solution

  • The problem might be the closing XML; from the HEREDOC syntax. It has to be at the start of the line, with no indentation allowed.

    If it is not at the start the HEREDOC element does not end. If WebMatrix recognizes the XML, the content after it is invalid content after the closing tag. Additionally HEREDOC allows variables (like a string in double quotes).

    In the most cases NOWDOC is a better solution:

    <?php
    
    $xml = <<<'XML'
    <some>
      <xml/>
    </some>
    XML;
    

    Additionally PHP strings can span multiple lines. You can use a single quoted string for your XML. Of course in this case you will have to escape single quotes and backslashes in the string.

    $xml = 
      '<some>
         <xml>with a single quote \' and a backslash \\</xml> 
       </some>';