What are the best practices for structuring PHP files to avoid header modification errors?

When structuring PHP files, it is important to ensure that no output is sent to the browser before modifying headers. To avoid header modification errors, it is recommended to organize your code in a way that headers are set before any output is generated, such as placing header functions at the beginning of the file or using output buffering.

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

// Set headers before any output
header("Content-Type: text/html");

// Your PHP code here

ob_end_flush(); // Flush output buffer
?>