What are some best practices for dynamically updating a menu based on folder names in PHP?

When dynamically updating a menu based on folder names in PHP, one best practice is to use the scandir() function to retrieve a list of folders in a specified directory. Then, iterate through the list of folders and generate menu items based on the folder names.

<?php
// Specify the directory containing the folders
$directory = 'path/to/directory';

// Get a list of folders in the specified directory
$folders = array_diff(scandir($directory), array('..', '.'));

// Generate menu items based on folder names
foreach ($folders as $folder) {
    echo '<a href="' . $folder . '">' . $folder . '</a>';
}
?>