How should the header function be used in PHP to avoid the "Cannot modify header information" error?

When using the `header()` function in PHP to set HTTP headers, it must be called before any actual output is sent to the browser. If the `header()` function is called after output has already been sent, it will result in the "Cannot modify header information" error. To avoid this error, make sure to call the `header()` function before any HTML, text, or whitespace is output to the browser.

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

// Place all header calls before any output
header('Location: https://www.example.com');
exit();

ob_end_flush(); // Flush the output buffer
?>