0

if I have a function with 1 param that is default, meaning if it isn't sent in the function call it get's it's value from the value I put in the function definition- like this:

function func(a$, $b, $c=null){}

I can call this function like this:

func(1,2); //$c get's null

or

func (1,2,3) ;//$c get's 3

I'm wondering if I can add another default param to this function like -

function func($a,$b,$c=null,$d=true){}

or it can cause a mixup if I call a func with params a,b and d (the d's value can go into $c by mistake right?)

is there a way around this?

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by blocblue
0

Yes, a function can have more than one default parameter value. You can call such functions as follows:

function func(1, 2, 3); // A, B, C

function func(1, 2, null, 4); // A, B, C (default), D

Notice that you have to explicitly pass the default value for C if you wish to set a value for D.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.