What are the best practices to avoid header-related issues in PHP, especially when dealing with includes or redirects?

When dealing with includes or redirects in PHP, it's important to ensure that no output is sent to the browser before headers are set. To avoid header-related issues, make sure that there are no spaces, line breaks, or other characters outside of the PHP tags before calling functions like header() or setcookie(). Additionally, using output buffering functions like ob_start() and ob_end_flush() can help prevent header errors.

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

// Your PHP code here

header("Location: https://www.example.com"); // Redirect example

ob_end_flush(); // Flush output buffer
?>