How can PHP be used to count the number of files in a directory?

To count the number of files in a directory using PHP, you can use the `scandir` function to scan the directory and then count the number of files returned in the array. This can be achieved by filtering out the "." and ".." entries that represent the current and parent directories.

$directory = "/path/to/directory";
$file_count = count(array_diff(scandir($directory), array('..', '.')));
echo "Number of files in directory: " . $file_count;