What are some best practices for handling header information in PHP, especially when dealing with dynamic content?

When handling dynamic content in PHP, it is important to set header information before any output is sent to the browser. This includes setting headers for content type, caching, redirection, etc. One common best practice is to use the header() function to set these headers before any HTML content is echoed or printed.

<?php
// Set content type header
header('Content-Type: text/html');

// Set caching headers
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

// Redirect to another page
header('Location: https://www.example.com');
exit;
?>