What best practices should be followed when using the header() function in PHP to avoid errors?

When using the header() function in PHP, it is important to ensure that no output has been sent to the browser before calling the function. This is because header() must be called before any actual output is sent. To avoid errors, make sure to call header() before any HTML tags, whitespace, or other content is echoed or printed to the browser.

<?php
// Ensure no output has been sent before calling header()
ob_start();
// Your code here
header("Location: https://www.example.com");
ob_end_flush();
?>