How can the header() function be used to redirect to a URL in PHP?

The header() function in PHP can be used to redirect users to a different URL by sending an HTTP header with the "Location" parameter set to the desired URL. This is commonly used for redirecting users after a form submission or when a specific condition is met. To redirect to a URL using the header() function, simply call the function with the "Location" parameter followed by the URL you want to redirect to.

<?php
// Redirect to a different URL
header("Location: https://www.example.com");
exit(); // Make sure to call exit() after the header to prevent further script execution
?>