How to Create and Read CSV Files with PHP
there is a function in php called fputcsv which helps us create a csv file.
<?php
$csv =fopen('example.csv','w+');
fputcsv($data);
fclose($csv);
as the first parameter, it gets the file we opened with fopen, and for the second one, we put the array we want to put into the CSV file.
it will be an array of arrays. each array represents a row in the CSV file. the first one is the header.
you can set the header and footer separately too.
$header = ['fname','lname','address','email'];
fputcsv($csv,$header);
$records = [];
foreach ($records as $record){
fputcsv($csv,$record);
}
and at last, we have to close the file with fclose() function.