views:

237

answers:

4

Hi all,

Is there a PHP function to find the number of parameters to be received/passed to a particular function?

+10  A: 

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
Anthony Forloney
Hi. Thanks, i have solved my issue w.r.t your answer.
VAC-Prabhu
+1  A: 

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

Boris Guéry
+2  A: 

Try func_num_args:

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

Paul
+1  A: 

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

}
Daniel