How can you sort folders alphabetically in PHP?

To sort folders alphabetically in PHP, you can use the scandir() function to retrieve the list of folders in a directory, then sort the array using the sort() function. This will rearrange the folders alphabetically.

$dir = '/path/to/directory';
$folders = scandir($dir);
sort($folders);

foreach ($folders as $folder) {
    if (is_dir($dir . '/' . $folder)) {
        echo $folder . "<br>";
    }
}