PHP Include or Require Gives Weird character in output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Custom24
Forum Newbie
Posts: 3
Joined: Thu Aug 19, 2010 6:49 pm

PHP Include or Require Gives Weird character in output

Post 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.
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: PHP Include or Require Gives Weird character in output

Post 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.
Custom24
Forum Newbie
Posts: 3
Joined: Thu Aug 19, 2010 6:49 pm

Re: PHP Include or Require Gives Weird character in output

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Include or Require Gives Weird character in output

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Custom24
Forum Newbie
Posts: 3
Joined: Thu Aug 19, 2010 6:49 pm

Re: PHP Include or Require Gives Weird character in output

Post 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.
Post Reply