What best practices should be followed when handling large amounts of data in PHP, especially when exporting to CSV format?
When handling large amounts of data in PHP, especially when exporting to CSV format, it is important to optimize memory usage and execution time. One way to achieve this is by using generators to process data in batches instead of loading all data into memory at once. This approach helps prevent memory exhaustion and improves performance when dealing with large datasets.
// Function to generate CSV data in batches using a generator
function generateCsvData($data) {
foreach ($data as $row) {
yield implode(',', $row) . PHP_EOL;
}
}
// Example usage
$csvData = generateCsvData($largeDataSet);
// Output CSV data to file
$fp = fopen('output.csv', 'w');
foreach ($csvData as $line) {
fwrite($fp, $line);
}
fclose($fp);