What potential issues can arise when using the opendir() function in PHP to count directories?

When using the opendir() function in PHP to count directories, one potential issue that can arise is not properly handling the "." and ".." directories, which are also returned by opendir(). To solve this issue, you can use a loop to iterate through the directories and exclude these special directories from the count.

$dir = "/path/to/directory";
$count = 0;

if ($handle = opendir($dir)) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && is_dir($dir . "/" . $entry)) {
            $count++;
        }
    }
    closedir($handle);
}

echo "Number of directories: " . $count;