What are the best practices for avoiding header-related errors in PHP scripts?

When working with PHP scripts, it's important to avoid header-related errors such as "Cannot modify header information - headers already sent." This error occurs when there is output (such as whitespace or HTML) before PHP sends headers. To avoid this issue, make sure to set headers before any output is sent to the browser.

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

// Your PHP code here
echo "Hello, World!";
?>