What are common issues when using fputcsv and fgetcsv in PHP to create and read CSV files?

One common issue when using fputcsv and fgetcsv in PHP is encountering formatting problems with the CSV file, such as extra spaces or incorrect delimiters. To solve this, you can specify the delimiter and enclosure characters when using fputcsv and fgetcsv functions.

// Specify delimiter and enclosure characters
$delimiter = ',';
$enclosure = '"';
$escape_char = "\\";

// Write to CSV file using fputcsv with specified delimiter and enclosure
$file = fopen('data.csv', 'w');
$data = array('John Doe', 'john@example.com', 'New York');
fputcsv($file, $data, $delimiter, $enclosure, $escape_char);
fclose($file);

// Read from CSV file using fgetcsv with specified delimiter and enclosure
$file = fopen('data.csv', 'r');
while (($row = fgetcsv($file, 0, $delimiter, $enclosure, $escape_char)) !== false) {
    var_dump($row);
}
fclose($file);