PHP Include function outputting unknown char

情到浓时终转凉″ 提交于 2019-12-06 05:25:01

问题


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.


回答1:


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




回答2:


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.




回答3:


It doesn't show up on the rendered page in Firefox or IE but you can see the funny character when you View Source in IE

Is this on a Linux machine? Could you do find & replace with vim or sed to see if you can get rid of the 3F that way?

If it's on Windows, try opening include.inc with Notepad to see if the funny char is visible & can be deleted.

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




回答4:


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.




回答5:


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.




回答6:


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).



来源:https://stackoverflow.com/questions/41647/php-include-function-outputting-unknown-char

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!