How can fputcsv() be utilized to write CSV data in PHP?

To write CSV data in PHP, you can use the fputcsv() function which formats a line as CSV and writes it to a file pointer. Simply open a file for writing in CSV format, create an array of data to be written, and then use fputcsv() to write the data to the file.

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

// Data to be written in CSV format
$data = array(
    array('John', 'Doe', 'john.doe@example.com'),
    array('Jane', 'Smith', 'jane.smith@example.com')
);

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

// Close the file
fclose($file);