$name = func_get_arg(func_get_args);
我试图获取我的 PHP 函数的最后一个传递的参数。
但是我没有给我最后一个参数,而是得到了这两个错误:
Notice: Use of undefined constant func_get_args - assumed 'func_get_args'
Warning: func_get_arg() expects parameter 1 to be long, string given
有人可以解释我为什么会发生这种情况以及如何解决它吗?
您正在尝试将函数名称作为参数传递给func_get_arg()
。这在 PHP 中永远行不通。
使用这个:
$arg = func_get_arg(func_num_args() -1);
或者,作为替代方案:
$arg = array_pop(func_get_args());
您可以使用以下命令获取最后一个参数
<?php
function foo()
{
$numargs = func_num_args();
$arg_list = func_get_args();
echo "Last argument: " . $arg_list[$numargs-1];
}
foo(1, 2, 3);
?>
// This will return last argument passed to the function
$lastArgument = func_get_arg(func_num_args()-1);