How can a dynamic switch statement be effectively implemented in PHP for handling menu items?

To implement a dynamic switch statement in PHP for handling menu items, you can use an associative array where the keys represent the menu item identifiers and the values represent the corresponding actions or functions to execute. This allows for a flexible and easily maintainable way to handle menu items without the need for multiple switch cases.

$menuItems = [
    'home' => 'displayHome',
    'about' => 'displayAbout',
    'contact' => 'displayContact'
];

$menuItemId = $_GET['menu_item_id']; // Assuming the menu item ID is passed as a query parameter

if (array_key_exists($menuItemId, $menuItems)) {
    $action = $menuItems[$menuItemId];
    $action(); // Call the corresponding function based on the menu item ID
} else {
    echo 'Invalid menu item ID';
}

function displayHome() {
    echo 'Home page content';
}

function displayAbout() {
    echo 'About page content';
}

function displayContact() {
    echo 'Contact page content';
}