What potential pitfalls should be avoided when using the header() function in PHP, especially in relation to form submissions?

When using the header() function in PHP, especially in relation to form submissions, it is important to avoid sending headers after content has already been output to the browser. This can cause errors such as "Headers already sent" and prevent the headers from being set properly. To avoid this issue, make sure to call the header() function before any output is sent to the browser.

<?php
// Correct way to use header() function before any output
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    // Process form submission
    // Redirect to another page after processing
    header('Location: success.php');
    exit;
}
?>