Are there any best practices for handling redirection in PHP scripts?

When handling redirection in PHP scripts, it is important to use the header() function to send HTTP headers for the redirection. This function should be called before any output is sent to the browser to avoid any errors. Additionally, it is recommended to use the correct HTTP status codes for different types of redirections, such as 301 for permanent redirects and 302 for temporary redirects.

// Redirect to a new page using a 301 status code for a permanent redirect
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://www.example.com/newpage.php");
exit;