How can the issue of modifying header information after output has started be avoided in PHP scripts?

Modifying header information after output has started can be avoided in PHP scripts by ensuring that no output is sent to the browser before setting headers. To solve this issue, use ob_start() at the beginning of the script to buffer the output, and then use ob_end_flush() at the end of the script to send the output to the browser.

<?php
ob_start();

// code that may generate output

header('Location: newpage.php');
exit();

ob_end_flush();
?>