What are best practices for structuring PHP scripts to ensure proper redirection without encountering header modification errors?

When working with PHP scripts that involve redirection, it is important to ensure that no output is sent to the browser before headers are modified for redirection. To avoid encountering header modification errors, it is recommended to structure your PHP scripts in a way that headers are set before any content is outputted.

<?php
// Set the header for redirection
header('Location: https://www.example.com');
exit; // Make sure to exit after setting the header
?>