我更喜欢__FILE__,因为它更容易打字和阅读。
PHP 对于确定当前脚本文件名的两种方式都有奇怪的行为。我基于此脚本的输出:
var_dump(__FILE__);
var_dump($_SERVER['SCRIPT_FILENAME']);
var_dump(realpath(__FILE__));
var_dump(realpath($_SERVER['SCRIPT_FILENAME']));
PHP 5.6 命令行、Windows、Cygwin:
string(40) "/cygdrive/c/file.php"
string(8) "file.php"
string(40) "/cygdrive/c/file.php"
string(40) "/cygdrive/c/file.php"
PHP 5.6 命令行、Windows、cmd:
string(31) "C:\file.php"
string(8) "file.php"
string(31) "C:\file.php"
string(31) "C:\file.php"
Apache + PHP 5.6,Windows:
string(31) "C:\file.php"
string(31) "C:/file.php"
string(31) "C:\file.php"
string(31) "C:\file.php"
PHP 5.4 命令行,Linux:
string(35) "/var/www/blah/file.php"
string(8) "file.php"
string(35) "/var/www/blah/file.php"
string(35) "/var/www/blah/file.php"
Nginx + PHP 5.4,Linux:
string(35) "/var/www/blah/file.php"
string(35) "/var/www/blah/file.php"
string(35) "/var/www/blah/file.php"
string(35) "/var/www/blah/file.php"
所以你必须根据你的情况来看看哪个是最好的。 :-)