How can PHP be used to handle errors when users input incorrect URLs?

When users input incorrect URLs, PHP can handle errors by using the header() function to redirect them to a custom error page. This custom error page can display a user-friendly message informing the user that the URL they entered is incorrect. By implementing this solution, users will have a better experience when encountering errors due to incorrect URLs.

<?php
if(!file_exists($_SERVER['DOCUMENT_ROOT'] . $_SERVER['REQUEST_URI'])) {
    header("Location: error_page.php");
    exit();
}
?>