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

When working with headers in PHP scripts, it's important to avoid sending headers after any content has already been output to the browser. To prevent errors, it's best practice to set all headers before any output is sent to the browser. One way to achieve this is by using the `header()` function at the beginning of your script before any HTML or text output.

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

// Your PHP code here
?>
<!DOCTYPE html>
<html>
<head>
    <title>Example Page</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>