Page 1 of 1

PHP Include or Require Gives Weird character in output

Posted: Thu Aug 19, 2010 7:14 pm
by Custom24
Hi
I noticed this in the last project I did, and ignored it, but it is causing illegal XML this time.

If I try to view the following page in IE or Chrome, both say the XML is illegal

Code: Select all

<?php
require("TestSetupVars.php");
header("Content-type: text/xml");
echo "<?xml version='1.0'?>";
echo "<note>";
echo "<from>Jani</from>";
echo "<to>Tove</to>";
echo "<message>Remember me this weekend</message>";
echo "</note>";
?>
TestSetupVars.php is just this;

Code: Select all

<?php
$a="a";
?>
If I remove the require, all is well.

Include does the same thing.

It looks as though the require is inserting a single character before the start of the xml document in the response output.

Chrome actually shows me a character (it looks like a dot) before the <note> in view source, but IE does not

This is happening on both my local machine (php 5.2.6) and the remote host (php 5.2.11). I can't upgrade the remote php version, so any ideas for a workaround?

The code I've posted here is to illustrate - in reality I need to require to do something useful, and rather than echoing out XML directly, I am using the DomDocument object.

Re: PHP Include or Require Gives Weird character in output

Posted: Thu Aug 19, 2010 7:37 pm
by MindOverBody
Smells like file character set problem. Try changing character sets to be equal on all files you include and one in which you incude.

Re: PHP Include or Require Gives Weird character in output

Posted: Fri Aug 20, 2010 4:34 am
by Custom24
Thank you, you were spot on.

I've been using Visual Studio 2010, and creating the php files as text files in this. It creates them as UTF-8. Changing the encoding to ANSI for both files (using programmer's notepad), and the problem goes away!

Thanks again.

Mark

Re: PHP Include or Require Gives Weird character in output

Posted: Fri Aug 20, 2010 10:27 am
by AbraCadaver
Custom24 wrote:Thank you, you were spot on.

I've been using Visual Studio 2010, and creating the php files as text files in this. It creates them as UTF-8. Changing the encoding to ANSI for both files (using programmer's notepad), and the problem goes away!

Thanks again.

Mark
You can keep them as UTF-8, but VS is saving a BOM (Byte Order Mark) at the beginning of the file. See if there is an option to not save the BOM.

Re: PHP Include or Require Gives Weird character in output

Posted: Fri Aug 20, 2010 10:52 am
by Custom24
Doesn't seem to be (I am using a mixture of IDE's for the project and it's just the Web Developer Express edition of VS).
I did find this, though

http://blog.activa.be/index.php/2008/09 ... al-studio/

I'm happy to fix them in Programmer's Notepad.