PHP $ _SERVER ['DOCUMENT_ROOT'] vs realpath(dirname(_FILE_))

I was wondering if one method was better than the other. I have been using the $_SERVER method to point to the current directory but is this method better than the realpath(dirname(_FILE_)) method or is there any difference?

The $_SERVER[ 'DOCUMENT_ROOT' ] variable returns a server setting. Specifically it returns:

The document root directory under which the current script is executing, as defined in the server's configuration file.

The realpath( dirname( __FILE__ ) ) function will return the path of the folder that the current script is actually residing.

One is returning the value of a server setting and the other evaluates to a file's path. In some cases, the values will be the same but you should be aware of the difference between the two.