What are the potential issues when exporting data to Excel in PHP without specifying the correct encoding?

When exporting data to Excel in PHP without specifying the correct encoding, special characters may not display correctly in the Excel file. To solve this issue, you should specify the correct encoding, such as UTF-8, when generating the Excel file.

// Set the correct encoding for the Excel file
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=export.csv');

// Output the data to the Excel file
$output = fopen('php://output', 'w');

// Write your data to the file
fputcsv($output, $data);

// Close the file
fclose($output);