In the context of PHP, how can header() function be utilized to redirect to a custom error page?

To redirect to a custom error page using the header() function in PHP, you can set the HTTP header status code to 301 or 302 for a temporary or permanent redirect, respectively. You can then specify the location of the custom error page in the header using the Location parameter.

<?php
// Set the HTTP status code to 301 for a temporary redirect
header("HTTP/1.1 301 Moved Permanently");
// Redirect to the custom error page
header("Location: custom_error_page.php");
exit();
?>