As @drew010 pointed out in the comments, your php.ini
probably has the short_open_tag
directive on. This causes <?
to be a valid open php tag, just like <?php
.
With this directive on, php would parse these the same way:
var str2 = '<?xml version="1.0" ?>';
var str2 = '<?php xml version="1.0" ?>';
If you can't turn that directive off because you don't have access or one or more components requires it to be on, this workaround should suffice:
var str2 = '<' + '?xml version="1.0" ?>';
The reason that the code worked when you changed the line to this:
var str2 = '<?xml?>';
is that php is treating xml
as a single statement. In this case the statement probably resolves to the string "xml"
after a lookup of any constant named xml
fails. Because you are not echoing the string, just evaluating it, your str2
variable should end up equating to ""
.