What is the best way to count the number of files in a directory using PHP?

To count the number of files in a directory using PHP, you can use the `scandir()` function to get an array of all the files in the directory, and then use `count()` to get the total number of files in the array. This method is efficient and straightforward.

$directory = '/path/to/directory';
$files = scandir($directory);
$num_files = count($files) - 2; // Subtract 2 to account for '.' and '..' entries

echo "Number of files in directory: " . $num_files;