What are the best practices for handling redirects and landing pages in PHP scripts to avoid parsing issues like the one described?

Issue: When handling redirects and landing pages in PHP scripts, it is important to ensure that no output is sent to the browser before performing the redirect. This can be achieved by using the `header()` function to send the redirect header before any other content is output.

<?php
// Check if a redirect is needed
if ($redirect_needed) {
    header("Location: https://www.example.com/new_page.php");
    exit;
}

// Output the landing page content
echo "<h1>Welcome to the landing page!</h1>";
?>