What is the correct usage of file_put_contents in PHP when exporting data to a CSV file?

When using file_put_contents to export data to a CSV file in PHP, it is important to properly format the data as a comma-separated string before writing it to the file. This can be achieved by using the implode function to join the data array elements with commas. Additionally, make sure to include the newline character "\n" at the end of each row to separate the rows in the CSV file.

$data = array(
    array('John', 'Doe', 'john.doe@example.com'),
    array('Jane', 'Smith', 'jane.smith@example.com'),
);

$csv = '';
foreach ($data as $row) {
    $csv .= implode(',', $row) . "\n";
}

file_put_contents('export.csv', $csv);