Search code examples
phpencodingbyte-order-mark

PHP Include function outputting unknown char


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.


Solution

  • 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