What is the common error message "Cannot modify header information - headers already sent" in PHP, and how can it be avoided?

The common error message "Cannot modify header information - headers already sent" in PHP occurs when there is output sent to the browser before PHP headers are set. To avoid this error, make sure there is no whitespace or output (including HTML, text, or even a stray space) before the `header()` function is called.

<?php
ob_start(); // Start output buffering
// Your PHP code here
ob_end_clean(); // Clean the output buffer without sending output
header("Location: newpage.php"); // Set the header after ensuring no output has been sent
exit(); // Exit to prevent further script execution
?>