Tuesday, November 13, 2007

Find out if a function was called with a parameter default value

A function can have optional arguments. They are determined to give it a default value defined. For example

function test ($key, $value = null)

But how can we differentiate whether this function is called with only one parameter, or if it is called using two but the second is equal to the default?
The solution is nested but not difficult... We simply need to find how many arguments were passed to the function when called


Function test ($key, $value = null)
{
If (func_num_args ()> 1) {
Echo '$value is sent';
}
}

test('1', null);
test('1');

No comments: