How can PHP scripts be optimized to handle special characters and line breaks in Excel CSV files for proper display?

Special characters and line breaks in Excel CSV files can be handled by properly encoding the data using functions like `utf8_encode()` and `str_replace()`. Additionally, using PHP's built-in functions like `fputcsv()` can help ensure that the data is formatted correctly for Excel.

// Sample code to handle special characters and line breaks in Excel CSV files

// Data to be written to CSV file
$data = array(
    array('Name', 'Age', 'City'),
    array('John Doe', 25, 'New York'),
    array('Jane Smith', 30, 'Los Angeles'),
    array('Mário Silva', 28, 'São Paulo'),
);

// Open a file for writing
$fp = fopen('data.csv', 'w');

// Loop through the data and write to the CSV file
foreach ($data as $row) {
    fputcsv($fp, array_map('utf8_encode', $row));
}

// Close the file
fclose($fp);