What are the potential pitfalls of using header() function in PHP to redirect to a new page?

One potential pitfall of using the header() function in PHP to redirect to a new page is that headers must be sent before any output is generated. If there is any output (such as HTML content or whitespace) before the header() function is called, it will result in a "headers already sent" error. To avoid this issue, you can use output buffering to capture any output before sending headers.

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

// Your PHP code here

if (condition) {
    header("Location: newpage.php");
    exit();
}

ob_end_flush(); // Flush the output buffer
?>