开发者

PHP function to find out the number of parameters passed into function?

开发者 https://www.devze.com 2022-12-30 09:34 出处:网络
Is there a PHP function to find the number of par开发者_C百科ameters to be received/passed to a particular function?func_num_args

Is there a PHP function to find the number of par开发者_C百科ameters to be received/passed to a particular function?


func_num_args

Gets the number of arguments passed to the function.

Here is an example taken right from the link above,

<?php
function foo()
{
    $numargs = func_num_args();
    echo "Number of arguments: $numargs\n";
}

foo(1, 2, 3);   
?>

Which outputs,

Number of arguments: 3


Try func_num_args:

http://www.php.net/manual/en/function.func-num-args.php


func_num_args() and func_get_args() to get the value of arguments

From the documentation :

<?php
function foo()
{
    $numargs = func_num_args();
    echo "Number of arguments: $numargs\n";
}

foo(1, 2, 3);   
?>

The above example will output:

Number of arguments: 3


func_number_args() is limited to only the function that is being called. You can't extract information about a function dynamically outside of the function at runtime.

If you're attempting to extract information about a function at runtime, I recommend the Reflection approach:

if(function_exists('foo'))
{
 $info = new ReflectionFunction('foo');
 $numberOfArgs = $info->getNumberOfParameters(); // this isn't required though
 $numberOfRequiredArgs = $info->getNumberOfRequiredParameters(); // required by the function

}
0

精彩评论

暂无评论...
验证码 换一张
取 消