tags:

views:

1013

answers:

9

When using the php include function the include is succesfully executed, but it is also outputting a char before the output of the include is outputted, the char is of hex value 3F and I have no idea where it is coming from, although it seems to happen with every include.

At first I thbought it was file encoding, but this doesn't seem to be a problem. I have created a test case to demonstrate it: (link no longer working) http://driveefficiently.com/testinclude.php this file consists of only:

<? include("include.inc"); ?>

and include.inc consists of only:

<? echo ("hello, world"); ?>

and yet, the output is: "?hello, world" where the ? is a char with a random value. It is this value that I do not know the origins of and it is sometimes screwing up my sites a bit.

Any ideas of where this could be coming from? At first I thought it might be something to do with file encoding, but I don't think its a problem.

A: 

I see hello, world on the page you linked to. No problems that I can see...

I'm using Firefox 3.0.1 and Windows XP. What browser/OS are you running? Perhaps that might be the problem.

Thomas Owens
A: 

Character 3F actually is the question mark, it isn't just displaying as one.

I get the same results as Thomas, no question mark showing up.

In theory it could be some problem with a web proxy but I am inclined to suspect a stray question mark in your PHP markup...which perhaps you have fixed by now so we don't see the problem.

Leigh Caldwell
A: 

I see hello, world on the page you linked to. No problems that I can see...

Leigh Caldwell & Thomas Owens, please do the following:

View the source in Firefox and copy and paste the entire contents (Ctrl-A it) and paste it into Notepad/Some editor (or even better something with a HEX editor) and you will see this "rouge" char.

DAC
+1  A: 
Mark Biek
+2  A: 

Your web server (or your text editor) apparently includes a BOM into the document. I don't see the rogue character in my browser except when I set the site's encoding explicitly to Latin-1. Then, I see two (!) UTF-8 BOMs.

/EDIT: From the fact that there are two BOMs I conclude that the BOM is actually included by your editor at the beginning of the file. What editor do you use? If you use Visual Studio, you've got to say “Save As …” in the File menu and then choose the button “Save with encoding …”. There, choose “UTF-8 without BOM” or something similar.

Konrad Rudolph
+7  A: 

What you are seeing is a UTF-8 Byte Order Mark:

The UTF-8 representation of the BOM is the byte sequence EF BB BF, which appears as the ISO-8859-1 characters  in most text editors and web browsers not prepared to handle UTF-8.

Byte Order Mark on Wikipedia

PHP does not understand that these characters should be "hidden" and sends these to the browser as if they were normal characters. To get rid of them you will need to open the file using a "proper" text editor that will allow you to save the file as UTF-8 without the leading BOM.

You can read more about this problem here

grapefrukt
A: 

I'd also be curious to see what happens if you copy the code out of the include and just run it by itself.

Mark: this is on a shared hosting solution, so I can not get shell access to the file. However, as you can see here, there are no characters that shouldn't be there, and running the same file as a script does not produce this char. (The shared hosting company have been of 0 help, continually telling me it is a browser issue).

DAC
A: 

@Konrad

Out of curiosity, how do you see the BOM? Is it in this somewhere? Never mind, I just saw your edit about setting the encoding on the page.

GET /testinclude.php HTTP/1.1
Host: driveefficiently.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1)     Gecko/2008070208 Firefox/3.0.1 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://stackoverflow.com/questions/41647/php-include-function-outputting-unknown-char

HTTP/1.1 200 OK
Date: Wed, 03 Sep 2008 13:45:51 GMT
Server: Apache
X-Powered-By: PHP/5.2.6
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

13 
......hello, world 
0
Mark Biek
A: 

@DAC Your .inc file has the BOM, open it using a hex-editor and you can see it.

grapefrukt