What are best practices for handling headers in PHP scripts to avoid errors?

When working with headers in PHP scripts, it is important to ensure that headers are sent before any output is generated. To avoid errors, it is recommended to use the `header()` function to set headers before any content is echoed or printed to the browser.

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

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