netdigger
netdigger

Reputation: 3789

Strange error when including file with UTF-8 WITH BOM

I have this really strange problem.

Whenever I try to include a file with "UTF-8" I get an error when running it. Even though the scripts run fully and returns the correct text.

(I do ini_set('default_charset', 'utf-8');)

Look at this example.. The code is like this:

When I have the code like this:

include_once $fn;
echo json_encode("ALL IS FINE");
die();

How an ajax respond interprets it: (As an error, but the response text is still the same, so it's actually always the correct text and the script runs fully..) enter image description here

and when I have the code like this:

echo json_encode("ALL IS FINE");
die();
include_once $fn;

How an ajax respond interprets it: enter image description here

I can get rid of the error if I use "ANSI as UTF-8" in Notepad++, which basically means UTF-8 without BOM. But I want to have it in utf8.

Upvotes: 0

Views: 423

Answers (1)

claustrofob
claustrofob

Reputation: 4984

Using utf8 with BOM in php you will get nothing but problems. In your case what you actually get in the ajax response is looks like this:

BOM"ALL IS FINE"

The BOM is out of the quotes so javascript fails to decode it because it is not a valid json anymore.

Moreover you will run into problems with BOM when using some modern php frameworks. Most of them are using output buffering to manipulate the content. The most common error in this case with BOM is

Warning: Cannot modify header information - headers already sent

Upvotes: 1

Related Questions