How can the SPL library be utilized to simplify the process of counting file extensions in PHP?

When counting file extensions in PHP, the SPL library can be utilized to simplify the process by providing built-in classes and iterators for handling file operations. By using the DirectoryIterator class, we can iterate through files in a directory and extract their extensions for counting.

$directory = new DirectoryIterator('/path/to/directory');
$extensions = [];

foreach ($directory as $file) {
    if ($file->isFile()) {
        $extension = $file->getExtension();
        if (!empty($extension)) {
            $extensions[$extension] = isset($extensions[$extension]) ? $extensions[$extension] + 1 : 1;
        }
    }
}

print_r($extensions);