What are common issues when using the header() function in PHP?

One common issue when using the header() function in PHP is that it must be called before any output is sent to the browser. If you try to set headers after output has already been sent, you will encounter an error. To solve this, make sure to call header() before any HTML or text is echoed or printed to the browser.

<?php
// Incorrect way - headers set after output
echo "Hello, World!";
header("Location: https://www.example.com");
exit;

// Correct way - headers set before output
header("Location: https://www.example.com");
exit;
?>