What is the correct order of operations to ensure successful CSV file download in PHP scripts?

When downloading a CSV file in PHP, it is important to set the appropriate headers before outputting the file contents. This ensures that the file is downloaded correctly without any issues. The correct order of operations involves setting the headers, outputting the file contents, and then exiting the script to prevent any further output.

<?php
// Set headers for CSV file download
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="example.csv"');

// Output the CSV file contents
echo "Name,Email\nJohn Doe,johndoe@example.com";

// Exit the script to prevent any further output
exit;
?>