PHP CLI:什么目录?

mac*_*264 3 php

我有一个从 / 执行的 php cli 脚本,但该脚本位于 /opt/script/script.php 中。如何从脚本中获取脚本的动态位置

$location = ... (Get the location of the script) 
echo $location == '/opt/script' ? 'YAY' : 'Stupid';
Run Code Online (Sandbox Code Playgroud)

输出

YAY
Run Code Online (Sandbox Code Playgroud)

那种事。

Álv*_*lez 6

dirname(__FILE__)(或__DIR__在较新版本中)应该足够了。您可以在Magic Constants章节找到更多参考。