Search code examples
phphtmlutf-8require-once

Using utf-8 in php and loading file (require_once) gives strange char


In place where I use require_once('something.php'); I have strange char in html and when I check page on validator:

Validation Output: 1 Error 
Error Line 1, Column 1: character "" not allowed in prolog 

This only happens when I'm using UTF-8. Earlier I have files in ANSI and it was ok.

Yes I'm changing meta and save all files in UTF-8 I can move code to section body but this is strange for me.

screenshot


Solution

  • This is most probably caused by the UTF-8 BOM (Byte Order Mark). Open any file in some HEX viewer / editor and check the first 3 bytes in that file.

    UTF-8 BOM in Windows-1250 encoding looks like this: . Or  in ISO-8859-1. That's EF BB BF in hexadecimal.

    Just save your files as UTF-8 without BOM. For example Notepad++ editor has both options under Format menu:

    • Convert into UTF-8 (without BOM)
    • Convert into UTF-8