How can PHP code be structured to effectively handle different URL paths using switch cases?

When handling different URL paths in PHP, using a switch case statement can be an effective way to route requests to the appropriate code block based on the URL. By checking the URL path and using switch cases to define the different possible paths, you can easily manage the logic for each specific route.

// Get the URL path
$url = $_SERVER['REQUEST_URI'];

// Use switch case to handle different URL paths
switch ($url) {
    case '/':
        // Code to handle the root path
        break;
    case '/about':
        // Code to handle the about page
        break;
    case '/contact':
        // Code to handle the contact page
        break;
    default:
        // Code to handle 404 page
        break;
}