What are the limitations of changing content-type in the middle of a document in PHP?

Changing the content-type in the middle of a document in PHP can cause issues such as headers already being sent. To solve this problem, you can use output buffering to capture the output before sending any headers, then change the content-type and output the content.

<?php

ob_start(); // Start output buffering

// Output content here

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

header('Content-Type: text/html'); // Change content-type if needed

echo $content; // Output the content

?>