What is the function pathinfo() used for in PHP and how does it differ from basename?

The function pathinfo() in PHP is used to parse a file path and return information about the path components such as directory name, base name, file extension, etc. On the other hand, basename() is used to return the base name of a file path. The main difference between the two is that pathinfo() returns an associative array containing information about the path, while basename() simply returns the base name of the file path.

// Example of using pathinfo() and basename()

$file_path = '/path/to/file.txt';

// Using pathinfo()
$path_info = pathinfo($file_path);
echo 'Directory: ' . $path_info['dirname'] . '<br>';
echo 'Base Name: ' . $path_info['basename'] . '<br>';
echo 'Extension: ' . $path_info['extension'] . '<br>';
echo 'File Name: ' . $path_info['filename'] . '<br>';

// Using basename()
$base_name = basename($file_path);
echo 'Base Name: ' . $base_name;