I have the following PHP function:
function func_name($name = 'John', $country = 'USA')
{
do something;
}
And now, am trying to pass variable to the function as follows:
func_name($name = 'Jack', $country = 'Brazil');
I know, we can pass it easily as func_name('jack', 'Brazil'); but the above function is just an example. The actual function has around 20 arguments and all have default values, and some are not at all passed to the function
So I would like to know if its proper to pass arguments as func_name($name = 'Jack', $country = 'Brazil');
No, it’s not the right way to do it.
foo($bar = 'baz')means you’re assigning'baz'to the variable$baras usual. This assignment operation results in the value that was assigned, so the expression$bar = 'baz'has the value'baz'. This value'baz'gets passed into the function. The functions doesn’t have any clue that you have assigned to a variable$bar.In other words, PHP doesn’t support named arguments.
If your function accepts many parameters, all or most of which are optional, use arrays: