Are there any best practices for creating expandable tree-like structures for displaying directories in PHP?

When creating expandable tree-like structures for displaying directories in PHP, one best practice is to use recursive functions to traverse the directory structure and generate the HTML markup for the tree. This allows for a flexible and dynamic way to display nested directories and files.

<?php
function displayDirectory($path) {
    echo '<ul>';
    $files = scandir($path);
    
    foreach($files as $file) {
        if ($file != '.' && $file != '..') {
            echo '<li>' . $file;
            
            if (is_dir($path . '/' . $file)) {
                displayDirectory($path . '/' . $file);
            }
            
            echo '</li>';
        }
    }
    
    echo '</ul>';
}

// Usage
$path = '/path/to/directory';
displayDirectory($path);
?>