How can PHP developers ensure that CSV files with UTF-8 encoding open correctly in Excel without manual intervention?

When opening CSV files with UTF-8 encoding in Excel, developers can ensure they open correctly by adding a BOM (Byte Order Mark) at the beginning of the file. This BOM signals to Excel that the file is UTF-8 encoded, preventing any garbled text or incorrect characters from appearing.

$csvData = "\xEF\xBB\xBF" . $csvData;
file_put_contents('file.csv', $csvData);