138

It looks the same for me,but I'm not sure,

because there are many projects that uses dirname(__FILE__).

user198729
  • 61,774
  • 108
  • 250
  • 348

1 Answers1

218

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
    • which is why 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
    • so, __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 to dirname(__FILE__).
This directory name does not have a trailing slash unless it is the root directory.
(Added in PHP 5.3.0.)

d-_-b
  • 21,536
  • 40
  • 150
  • 256
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • When is `__FILE__` available? – user198729 May 01 '10 at 09:36
  • 3
    Not sure, but it's been there for quite some time *(the Magic constants page of the manual says its bahavior has been modified in PHP 4.0.2 ; so I would say it's been there before)* – Pascal MARTIN May 01 '10 at 09:43
  • 3
    __DIR__ and dirname(__FILE__) always strip the last slash, correct? – nkkollaw May 29 '13 at 23:12
  • 3
    Correct, except when the whole path is "/", in which case they don't strip that. – Francisco Presencia Jan 03 '14 at 04:15
  • 1
    I'm not sure if I understood correctly: **dirname(__FILE__)** is the fastest one, right? – Giacomo Pigani Mar 16 '14 at 11:46
  • 8
    @GiacomoTecyaPigani no, `__DIR__` is faster. – Nico Mar 17 '14 at 17:46
  • I am pretty sure that `__DIR__` and dirname(`__FILE__`) have different outputs under symlinks, but I haven't tested it yet – Steel Brain Oct 09 '14 at 04:47
  • 1
    With a simple benchmark, executing it 100,000,000 times, I get 17s for dirname(__FILE__) and 4-3s for __DIR__, consistently across 5 tests, so it looks like the difference is considerable (albeit divided by 100,000,000). – ywarnier Feb 17 '15 at 15:57
  • @user198729, `__FILE__` and `__LINE__` has been around a looong time. They even work properly on [3.0.11](http://museum.php.net/win32/).... – Pacerier Aug 11 '15 at 07:22
  • @ywarnier, Which engine did you use? – Pacerier Aug 11 '15 at 07:23
  • @PascalMARTIN, Is there anything stopping the compiler/interpreter (e.g. HipHop vm) from evaluating the string `dirname(__FILE__)` as a synonym as `__DIR__`? – Pacerier Aug 11 '15 at 07:23
  • @Pacerier I believe I used PHP 5.5 on the command line. I did the test again with PHP 5.6 CLI. Same result, more or less (factor 4). – ywarnier Dec 17 '15 at 18:13
  • 5
    Calling `__DIR__` 1 million times: 0.005666971206665 seconds. Calling `dirname(__FILE__)` 1 million times: 0.062714099884033 - either way, performance is not the problem, I personally like `__DIR__` better for the readability and for being more concise. – Lucas Bustamante Sep 28 '20 at 13:01
  • @SteelBrain I tried to reproduce your concern, but it seems `dirname(__FILE__) === __DIR__` was true for all my tests, linking files, linking directories, calling from outside, inside, etc. – Valerio Bozz May 10 '23 at 10:17