How can PHP developers efficiently handle errors related to missing pages without repeating code in multiple switch cases?

To efficiently handle errors related to missing pages without repeating code in multiple switch cases, PHP developers can create a custom function to handle the error message and redirect the user to a designated error page. By centralizing the error handling logic in one function, developers can easily call this function whenever a missing page error occurs, reducing code repetition and improving maintainability.

function handleMissingPageError(){
    header("HTTP/1.0 404 Not Found");
    include('error_page.php');
    exit();
}

// Implementing the custom error handling function in a switch case
$page = $_GET['page'] ?? '';

switch($page){
    case 'home':
        include('home.php');
        break;
    case 'about':
        include('about.php');
        break;
    default:
        handleMissingPageError();
}