How can the pathinfo() function be used to extract file extensions in PHP?

To extract file extensions in PHP, the pathinfo() function can be used. This function takes a file path as input and returns an associative array containing information about the file, including the file extension. By accessing the 'extension' key of the array, we can easily retrieve the file extension.

$file_path = '/path/to/file/example.txt';
$file_info = pathinfo($file_path);
$file_extension = $file_info['extension'];

echo $file_extension; // Outputs 'txt'