Are there any best practices for formatting CSV output in PHP to avoid issues like extra characters or incorrect formatting?

When outputting data to a CSV file in PHP, it is important to properly format the data to avoid issues such as extra characters or incorrect formatting. One common best practice is to use the `fputcsv()` function, which automatically formats the data and encloses it in quotes if necessary. Additionally, make sure to properly handle special characters by using functions like `utf8_encode()`.

// Sample data to output to CSV
$data = [
    ['John Doe', 'john.doe@example.com'],
    ['Jane Smith', 'jane.smith@example.com'],
];

// Open the file handle
$fp = fopen('output.csv', 'w');

// Output data to CSV file using fputcsv
foreach ($data as $row) {
    fputcsv($fp, $row);
}

// Close the file handle
fclose($fp);