What are some potential pitfalls to avoid when manipulating CSV files in PHP?

One potential pitfall when manipulating CSV files in PHP is not properly handling the encoding of the file. If the CSV file contains special characters or is encoded in a different format, it can lead to data corruption or incorrect parsing. To avoid this issue, always specify the correct encoding when reading or writing CSV files in PHP.

// Specify the encoding when reading CSV file
$file = fopen('data.csv', 'r');
while (($data = fgetcsv($file, 1000, ',', '"', 'UTF-8')) !== false) {
    // Process the CSV data
}
fclose($file);

// Specify the encoding when writing CSV file
$file = fopen('data.csv', 'w');
$data = ['John Doe', 'john.doe@example.com'];
fputcsv($file, $data, ',', '"', 'UTF-8');
fclose($file);