1

I have a index.php file:

<?php
// Load resources shared across the website
include '../path/to/other_file.php';
?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  xml:lang="es" lang="es" prefix="og: http://ogp.me/ns#">
<head prefix="article: http://ogp.me/ns/article#">
    <title>Title</title>
</head>

<body>

<!-- Code -->

</body>
</html>

When I load it, it shows nothing. I used this online tool to see what's happening. The problem occurs at line 5.

Probably it's that I'm using <? to introduce the XHTML document, but I guess PHP thinks the following word would be php (<?php //CODE ?>) instead of xml (<?xml version="1.0" encoding="UTF-8"?>)

How can I fix it? I think it's called "parsing" or "espacing" the characters.

0

3 Answers 3

3

You can fix this problem using a simple echo :

include '../path/to/other_file.php';
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
Sign up to request clarification or add additional context in comments.

Comments

2

The problem occurs because PHP thinks you're trying to enter "PHP mode" when you're doing <?, when in fact you're trying to access XML. The simple solution to this is to disable what's called "PHP short tags", where PHP recognizes <? as a valid opening-tag.

You can do this by various methods, all which effectively do the same

  1. Add short_open_tag=Off in your php.ini
  2. Add ini_set('short_open_tag', '0'); at the top of your specified file
  3. In php_value short_open_tag 0 in your .htaccess
  4. Use <?php echo "<?xml ... ?>"; to add your XML.

2 Comments

Not 2 - short_open_tag is PHP_INI_PERDIR and can't be changed with ini_set(): php.net/manual/en/ini.core.php
Really, @AbraCadaver? TIL! I'll strike over it!
0

Try to send the browser, that the php file is an xml file with this line:

header("Content-type: text/xml");

put this right after you start the php tag, so script can override the header

2 Comments

The problem is happening on the server, because PHP doesn't understand <?xml in a PHP script.
While sending a MIME type header is certainly necessary for an XHTML file (otherwise the browser will think it's an HTML file), this is the the problem the OP is asking about. By the way, application/xhtml+xml is the preferred one, although text/xml will work just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.