In what scenarios would using glob be more beneficial than scandir for file filtering in PHP?

When you need to filter files based on a pattern or wildcard, using glob in PHP can be more beneficial than scandir. Glob allows you to specify a pattern to match files against, making it easier to filter files based on their names. This can be especially useful when you need to filter files by their extension or a specific naming convention.

$files = glob('/path/to/directory/*.txt');
foreach ($files as $file) {
    echo $file . "\n";
}