How to avoid echoing character 65279 in php?

To conclude, and specify the solution:

Windows Notepad adds the BOM character (the 3 bytes: EF BB BF) to files saved with utf-8 encoding.

PHP doesn't seem to be bothered by it - unless you include one php file into another - then things get messy and strings gets displayed with character(65279) prepended to them.

You can edit the file with another text editor such as Notepad++ and use the encoding
"Encode in UTF-8 without BOM",
and this seems to fix the problem.

Also, you can save the other php file with ANSI encoding in notepad - and this also seem to work (that is, in case you actually don't use any extended characters in the file, I guess...)


If you want to print a string that contains the ZERO WIDTH NO-BREAK SPACE char (e.g., by including an external non-PHP file), try the following code:

echo preg_replace("/\xEF\xBB\xBF/", "", $string);

If you are using Linux or Mac, here is an elegant solution to get rid of the  character in PHP.

If you are using WordPress (25% of Internet websites are powered by WordPress), the chances are that a plugin or the active theme are introducing the BOM character due a file that contains BOM (maybe that file was edited in Windows). If that's the case, go to your wp-content/themes/ folder and run the following command:

grep -rl $'\xEF\xBB\xBF' .

This will search for files with BOM. If you have .php results in the list, then do this:

  1. Rename the file to something like filename.bom.bak.php
  2. Open the file in your editor and copy the content in the clipbard.
  3. Create a new file and paste the content from the clipboard.
  4. Save the file with the original name filename.php

If you are dealing with this locally, then eventually you'd need to re-upload the new files to the server.

If you don't have results after running the grep command and you are using WordPress, then another place to check for BOM files is the /wp-content/plugins folder. Go there and run the command again. Alternatively, you can start deactivating all the plugins and then check if the problem is solved while you active the plugins again.

If you are not using WordPress, then go to the root of your project folder and run the command to find files with BOM. If any file is found, then run the four steps procedure described above.