What potential pitfalls should be considered when using header location in PHP?

One potential pitfall when using header location in PHP is the risk of "headers already sent" errors if there is any output (such as HTML, whitespace, or error messages) before the header function is called. To prevent this, make sure to call the header function before any output is sent to the browser.

<?php
// Correct way to use header location in PHP
ob_start(); // Start output buffering
header('Location: http://www.example.com');
ob_end_flush(); // Flush the output buffer
exit;
?>