How can the presence of output before the header() function affect its functionality in PHP scripts?
When there is output before the header() function in PHP scripts, it can cause a "headers already sent" error because headers must be sent before any output is generated. To solve this issue, you need to ensure that there is no output (including whitespace) before the header() function is called.
<?php
ob_start(); // Start output buffering
// Output code here
header('Location: http://www.example.com');
exit();
ob_end_flush(); // Flush the output buffer
?>