What are the potential pitfalls of using substr() to extract a substring from an array of file names in PHP?

Using substr() to extract a substring from an array of file names in PHP can lead to errors if the file names have varying lengths. To avoid this issue, it is recommended to use pathinfo() function to extract the filename from a file path.

$filePaths = [
    "/path/to/file1.txt",
    "/path/to/anotherfile2.txt",
    "/path/to/file3.txt"
];

foreach($filePaths as $filePath) {
    $fileName = pathinfo($filePath, PATHINFO_FILENAME);
    echo $fileName . "\n";
}