In what ways can the PHP code be optimized to simplify the logic and functionality of displaying directory contents in a web interface?

To simplify the logic and functionality of displaying directory contents in a web interface, we can use the `scandir()` function to retrieve a list of files and directories in a specified directory. We can then loop through the list and display each item as a link in the web interface.

$dir = 'path/to/directory';

$files = scandir($dir);

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