How does realpath() function differ from dirname() function in PHP?

realpath() function in PHP resolves any symbolic links or relative path components in a given path and returns the absolute path. On the other hand, dirname() function in PHP only returns the directory component of a given path, without resolving any symbolic links or relative path components. If you need the absolute path of a file or directory, you should use realpath(). If you only need the directory component of a path, you can use dirname().

// Example using realpath()
$path = '/var/www/html/../public_html/index.php';
$absolutePath = realpath($path);
echo $absolutePath;

// Example using dirname()
$path = '/var/www/html/../public_html/index.php';
$directory = dirname($path);
echo $directory;