What are some best practices to avoid the error of modifying header information in PHP?

Modifying header information in PHP can lead to unexpected behavior and errors, such as the "Headers already sent" warning. To avoid this issue, it's best practice to ensure that no output is sent to the browser before modifying headers. One way to achieve this is by using output buffering to capture any output before sending headers.

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

// Your PHP code here

ob_end_flush(); // Send output and turn off output buffering