What is the purpose of an autoloader in PHP and how can it help with including files in different locations?

When working with PHP projects that have multiple files and directories, manually including each file can be cumbersome and error-prone. An autoloader in PHP helps to automatically load classes or files based on their names, making it easier to manage dependencies and include files from different locations.

// Autoloader function to automatically include classes based on their names
spl_autoload_register(function ($class_name) {
    include $class_name . '.php';
});

// Usage example: When a class is instantiated, the autoloader will include the file if needed
$obj = new MyClass();