What are some best practices for using the header function in PHP for redirection?

When using the header function in PHP for redirection, it is important to ensure that no output has been sent to the browser before calling the function. This can be achieved by using output buffering or ensuring that there are no HTML tags or whitespace before the header function is called. Additionally, it is recommended to use the "Location" header to specify the URL to redirect to and to include an exit or die statement after the header function to prevent any further code execution.

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

// Your PHP code here

header("Location: https://www.example.com"); // Redirect to the specified URL
exit; // Stop further execution
ob_end_flush(); // Flush output buffer
?>