What are common issues with the header() function in PHP scripts?

One common issue with the header() function in PHP scripts is that it must be called before any actual output is sent to the browser. If there is any whitespace or HTML content before the header() function is called, it will result in a "headers already sent" error. To solve this issue, make sure to call the header() function before any output, including whitespace or HTML content.

<?php
ob_start(); // Start output buffering
header("Location: https://www.example.com"); // Redirect to example.com
exit(); // Stop script execution
ob_end_flush(); // Flush output buffer
?>