Example 1 — Match a File Extension
Check whether a file name ends with .txt.
<?php
$name = 'notes.txt';
if (fnmatch('*.txt', $name)) {
echo "$name is a text file", PHP_EOL;
} else {
echo "$name is not a text file", PHP_EOL;
} How It Works
- The pattern *. txt means any text followed by .txt.
- fnmatch() compares the pattern with notes. txt and finds a match.
- It returns true, so the first branch runs.
- Nothing is read from the disk, because only the name is tested.

