How can the contents of a directory be sorted and outputted in PHP?

To sort the contents of a directory in PHP, you can use the `scandir()` function to retrieve the list of files in the directory, then use `sort()` function to sort the array of files. Finally, you can loop through the sorted array and output the files.

$directory = "/path/to/directory";
$files = scandir($directory);
sort($files);

foreach($files as $file){
    if($file != '.' && $file != '..'){
        echo $file . "<br>";
    }
}