Hi all,
Is there a PHP function to find the number of parameters to be received/passed to a particular function?
Hi all,
Is there a PHP function to find the number of parameters to be received/passed to a particular function?
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
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
}