Skip to content
PHP Logo

What are the potential drawbacks of creating separate folders and files for each page in a PHP website?

Having separate folders and files for each page in a PHP website can lead to a cluttered directory structure, making it difficult to manage and maintain the codebase. To solve this issue, you can organize your files into logical directories based on functionality or modules, and use a routing system to map URLs to the appropriate PHP files.

php
// index.php

// Define a routing system to map URLs to PHP files
$routes = [
    '/' => 'home.php',
    '/about' => 'about.php',
    '/contact' => 'contact.php',
    // Add more routes as needed
];

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

// Check if the URL is in the routes array
if (array_key_exists($url, $routes)) {
    // Include the corresponding PHP file
    include $routes[$url];
} else {
    // Handle 404 error
    include '404.php';
}