What are the benefits of using is_dir() and count() functions when working with directories in PHP?

When working with directories in PHP, it is important to check if a directory exists and to count the number of items within a directory. The is_dir() function can be used to check if a directory exists, while the count() function can be used to count the number of items within a directory.

// Check if a directory exists
if (is_dir('path/to/directory')) {
    echo 'Directory exists';
} else {
    echo 'Directory does not exist';
}

// Count the number of items in a directory
$directory = 'path/to/directory';
$files = scandir($directory);
$num_files = count($files) - 2; // Subtract 2 to exclude . and ..
echo 'Number of items in directory: ' . $num_files;