1

Can any one tell which function to use to determine the number of arguments passed within a function that has optional arguments?

1 Answer 1

6

You mean func_num_args? That will count the passed arguments, but not those that are not passed and get the default value:

function foo($a, $b='bar') {
   echo func_num_args() . PHP_EOL;
}

foo(1);
foo(1,2);
foo(1,'bar');

prints

1
2
2

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

I think he wants to test if a function xyz($a, $b='') was called like xyz(1) or xyz(1, 'foo')
@ThiefMaster: Isn't that what func_num_arg is doing? Or I don't get your point...
Indeed. I thought func_num_args counts all arguments.. no matter if default or passed manually.

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.