UTF-8 BOM: What It Is and When It Causes Problems

Last updated: August 28, 2026.

A UTF-8 byte order mark is the three-byte sequence EF BB BF at the beginning of a file. UTF-8 does not require byte-order information, although some editors add the sequence as a signature.

Common symptoms

Browsers usually tolerate a BOM in HTML. It is more troublesome when bytes must begin with exact syntax.

  • PHP reports output before headers.
  • A CSV importer sees an invisible prefix in the first header.
  • A strict JSON consumer rejects the document.
  • Concatenated files contain BOM bytes in the middle.

Remove it only at byte zero

Do not delete the same byte sequence elsewhere in the file.

<?php
$data = file_get_contents(__DIR__ . '/input.txt');
if ($data === false) throw new RuntimeException('Unable to read file.');
$bom = "\xEF\xBB\xBF";
if (str_starts_with($data, $bom)) {
    $data = substr($data, 3);
}
file_put_contents(__DIR__ . '/without-bom.txt', $data, LOCK_EX);

Choose deliberately

UTF-8 without BOM is normally the least surprising choice for web source and PHP files. If another system requires a BOM, add it only at that export boundary and test the receiver.

Reference: Unicode byte order mark FAQ.

admin

admin