What role does the header function play in redirecting URLs in PHP scripts?

The header function in PHP is used to send a raw HTTP header to the client. It is commonly used for URL redirection by sending a "Location" header with the new URL. This allows the server to redirect the user to a different page or URL.

<?php
// Redirect to a new URL
header('Location: https://www.example.com/newpage.php');
exit;
?>