How can PHP be used to offer CSV files for download to clients?

To offer CSV files for download to clients using PHP, you can create a PHP script that sets the appropriate headers to indicate that the content being served is a CSV file and then outputs the CSV data. This allows clients to download the file directly from the browser.

<?php
// Set headers to indicate that the content is a CSV file
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="data.csv"');

// Output CSV data
echo "Name,Email\n";
echo "John Doe,johndoe@example.com\n";
echo "Jane Smith,janesmith@example.com\n";
echo "Bob Johnson,bobjohnson@example.com";
?>