How can the "Cannot modify header information" error be resolved when using the header() function in PHP?

The "Cannot modify header information" error in PHP occurs when there is output sent to the browser before the header() function is called. To resolve this issue, ensure that there is no whitespace, HTML, or any other output before calling the header() function.

ob_start();
// Any output or HTML content should be placed after ob_start()

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