How can the use of header() function affect the execution of a PHP script, especially when used for redirection?

The use of the header() function in PHP can affect the execution of a script, especially when used for redirection, as it sends an HTTP header to the browser before any other output. To avoid issues like "headers already sent" errors, it's important to ensure that no output is sent to the browser before using the header() function for redirection.

<?php
ob_start(); // Start output buffering

// Your PHP code here

header("Location: newpage.php"); // Redirect to new page

ob_end_flush(); // Flush the output buffer
exit; // Ensure that no further code is executed after redirection
?>