What are some best practices for avoiding the "Cannot modify header information" warning in PHP?

When encountering the "Cannot modify header information" warning in PHP, it is typically caused by attempting to modify header information after output has already been sent to the browser. To avoid this warning, make sure to set headers before any other output is sent to the browser, such as HTML content or whitespace.

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

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

// Output content
echo 'Hello, World!';

ob_end_flush(); // Flush output buffer
?>