__DIR__和PHP中的目录名(__FILE__)之间有什么区别吗? [英] Is there any difference between __DIR__ and dirname(__FILE__) in PHP?
问题描述
对我来说看起来一样,但我不确定
It looks the same for me,but I'm not sure,
因为有许多项目使用dirname(__FILE__)
.
推荐答案
结果完全相同;所以,没什么区别.
Their result is exactly the same ; so, no difference on that.
例如,以下两行:
For example, the two following lines :
var_dump(dirname(__FILE__));
var_dump(__DIR__);
都将给出相同的输出:
string '/home/squale/developpement/tests/temp' (length=37)
但是,至少有两个区别:
But, there are at least two differences :
-
__DIR__
仅在 PHP> = 5.3 时存在- 这就是为什么
dirname(__FILE__)
被更广泛地使用的原因
__DIR__
only exists with PHP >= 5.3- which is why
dirname(__FILE__)
is more widely used
- 因此,
__DIR__
更快(或者应该是).
作为参考,请参见 魔术常数手册(引用) 部分:
As, as a reference, see the Magic constants section of the manual (quoting) :__DIR__
:文件的目录.
如果在包含中使用,则 包含文件的目录是 回来.
这相当于dirname(__FILE__)
.
这 目录名称没有 除非是根,否则末尾斜杠 目录.
(PHP 5.3.0中添加.)__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.)这篇关于__DIR__和PHP中的目录名(__FILE__)之间有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- which is why
- 这就是为什么