How can the use of BOM (Byte Order Mark) affect the encoding of exported data in PHP for Excel?

When exporting data to Excel in PHP, the use of a BOM (Byte Order Mark) can affect the encoding of the exported data, causing issues with special characters or incorrect formatting. To solve this issue, you can remove the BOM from the exported file by ensuring that the output is UTF-8 encoded without the BOM.

// Set the content type and charset for the exported file
header('Content-Type: application/vnd.ms-excel; charset=UTF-8');

// Output the data to a file without the BOM
$output = "\xEF\xBB\xBF" . $data; // Add BOM to the data
$output = mb_substr($output, 3); // Remove BOM
echo $output;