What is the purpose of an autoloader in PHP and how does it work?

The purpose of an autoloader in PHP is to automatically load classes when they are needed, without the need to manually include each class file. This helps to improve code organization and reduce the amount of boilerplate code required for class loading.

spl_autoload_register(function($class) {
    include 'path/to/classes/' . $class . '.php';
});