Are there any limitations or restrictions when trying to add a header in a CSV file using fputcsv?

When using fputcsv to add a header in a CSV file, there are no built-in functions to directly add a header row. One way to work around this limitation is to manually write the header row before using fputcsv to write the data rows. This involves opening the file for writing, writing the header row using fputcsv, and then writing the data rows.

<?php
$filename = 'data.csv';
$header = ['Name', 'Age', 'City'];

// Open file for writing
$file = fopen($filename, 'w');

// Write header row
fputcsv($file, $header);

// Write data rows
$data = [
    ['John Doe', 30, 'New York'],
    ['Jane Smith', 25, 'Los Angeles'],
];

foreach ($data as $row) {
    fputcsv($file, $row);
}

// Close file
fclose($file);
?>