What are the limitations or constraints when trying to generate new HTML content while simultaneously initiating a file download response in PHP?

When trying to generate new HTML content while simultaneously initiating a file download response in PHP, the main limitation is that you can only have one type of response per request. To solve this issue, you can use output buffering to capture the HTML content before sending the file download response.

<?php
ob_start(); // Start output buffering

// Generate HTML content here
echo "<html><body><h1>Hello World!</h1></body></html>";

$htmlContent = ob_get_clean(); // Get the buffered HTML content

// Send file download response
header('Content-Disposition: attachment; filename="example.txt"');
header('Content-Type: text/plain');
echo "This is the content of the file.";

// Output the HTML content
echo $htmlContent;