The dirname() function in PHP is an inbuilt function which is used to return the directory name of a given path. The dirname() function is used to parent directory's path i.e levels up from the current directory. The dirname() function returns the path of a parent directory which includes a dot ('.
The __FILE__ constant returns full path and name of the PHP file that's being executed. If used inside an include, the name of the included file is returned.
__FILE__ is simply the name of the current file. realpath(dirname(__FILE__)) gets the name of the directory that the file is in -- in essence, the directory that the app is installed in. And @ is PHP's extremely silly way of suppressing errors.
Their result is exactly the same ; so, no difference on that.
For example, the two following lines :
var_dump(dirname(__FILE__));
var_dump(__DIR__);
Will both give the same output :
string '/home/squale/developpement/tests/temp' (length=37)
But, there are at least two differences :
__DIR__
only exists with PHP >= 5.3
dirname(__FILE__)
is more widely used__DIR__
is evaluated at compile-time, while dirname(__FILE__)
means a function-call and is evaluated at execution-time
__DIR__
is (or, should be) faster.
As, as a reference, see the Magic constants section of the manual (quoting) :
__DIR__
: The directory of the file.
If used inside an include, the directory of the included file is returned.
This is equivalent todirname(__FILE__)
.
This directory name does not have a trailing slash unless it is the root directory.
(Added in PHP 5.3.0.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With