PHP:输出当前脚本名

PHP: Output current script name

为什么是下面的代码:

echo "Usage: " basename($_SERVER["SCRIPT_FILENAME"], '.php') "<arg2> <arg1>";

产生以下语法错误:

PHP Parse error: syntax error, unexpected 'basename' (T_STRING), expecting ',' or ';'

您应该连接 . 运算符以将字符串作为 1 个参数提供给 echo:

echo "Usage: " . basename($_SERVER["SCRIPT_FILENAME"], '.php') . "<arg2> <arg1>";

使用,提供多个:

echo "Usage: ", basename($_SERVER["SCRIPT_FILENAME"], '.php'), "<arg2> <arg1>";
echo __FILE__; //to get the current filename.

因此您的代码变为:

if($argc!=3){
   echo "Usage: ".__FILE__.".php <arg2> <arg1>";
   die;
}

你也可以使用逗号,像这样:

if ($argc != 3) {
    echo "Usage:", basename($_SERVER["SCRIPT_FILENAME"]), '.php', "<arg2> <arg1>";
    die;
}