gpt4 book ai didi

php - PHP 中的 __DIR__ 和 dirname(__FILE__) 有什么区别吗?

转载 作者:IT老高 更新时间:2023-10-28 11:48:41 25 4
gpt4 key购买 nike

对我来说看起来一样,但我不确定,

因为有很多项目使用dirname(__FILE__)

最佳答案

他们的结果完全一样 ;所以,没有区别。


例如以下两行:

var_dump(dirname(__FILE__));
var_dump(__DIR__);

两者都会给出相同的输出:

string '/home/squale/developpement/tests/temp' (length=37)


但是,至少有两个区别:

  • __DIR__ 仅存在于 PHP >= 5.3
    • 这就是 dirname(__FILE__) 被更广泛使用的原因
  • __DIR__ 在编译时进行评估,而 dirname(__FILE__) 表示函数调用和在执行时评估
    • 所以,__DIR__ (或者,应该) 更快。​​


作为引用,请参阅 Magic constants手册部分(引用):

__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.)

关于php - PHP 中的 __DIR__ 和 dirname(__FILE__) 有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2749416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com