Member Avatar for arcticM

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.