How can one efficiently iterate through a directory to load and list plugins in PHP?

To efficiently iterate through a directory to load and list plugins in PHP, you can use the opendir() function to open the directory, readdir() function to read each file in the directory, and then filter out only the PHP files. You can then include or require each PHP file to load the plugins.

$directory = 'plugins/';
$files = scandir($directory);

foreach($files as $file){
    if(pathinfo($file, PATHINFO_EXTENSION) == 'php'){
        include $directory . $file;
        echo $file . " loaded.<br>";
    }
}