How can one prevent the error of modifying header information in PHP?

Modifying header information in PHP can lead to errors, such as "Headers already sent" warnings. To prevent this issue, make sure to set any header information before any output is sent to the browser. This can be achieved by placing header functions at the beginning of the PHP script, before any HTML or whitespace.

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

// Set header information before any output
header('Content-Type: text/html');

// Rest of the PHP code goes here

ob_end_flush(); // Flush the output buffer
?>