In PHP, what are the advantages of using dirname(__FILE__) over $_SERVER['DOCUMENT_ROOT'] for obtaining directory paths?

When obtaining directory paths in PHP, using dirname(__FILE__) is preferred over $_SERVER['DOCUMENT_ROOT'] because dirname(__FILE__) gives the absolute path of the current file, while $_SERVER['DOCUMENT_ROOT'] gives the document root directory of the server. This ensures that the path obtained using dirname(__FILE__) is consistent regardless of server configuration, making the code more portable.

// Using dirname(__FILE__) to get the absolute path of the current file
$dir_path = dirname(__FILE__);
echo $dir_path;