What are the best practices for using the header() function in PHP to avoid errors related to modifying header information?

When using the header() function in PHP to modify HTTP headers, it is important to ensure that no output has been sent to the browser before calling the function. To avoid errors related to modifying header information, you should always call the header() function before any output is generated, such as HTML content or whitespace.

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

// Your PHP code here

header('Location: https://www.example.com'); // Modify header information

ob_end_flush(); // Flush output buffer
?>