0

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

4 Answers 4

12

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
Sign up to request clarification or add additional context in comments.

Comments

2

Try func_num_args:

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

Comments

1

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

Comments

1

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.