Prabhu Murthi
Prabhu Murthi

Reputation: 2876

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

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

Upvotes: 0

Views: 4458

Answers (4)

Daniel
Daniel

Reputation: 870

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

}

Upvotes: 1

Paul Degnan
Paul Degnan

Reputation: 2002

Try func_num_args:

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

Upvotes: 2

Boris Guéry
Boris Guéry

Reputation: 47585

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

Upvotes: 1

Anthony Forloney
Anthony Forloney

Reputation: 91786

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

Upvotes: 12

Related Questions