Page 1 of 1

Passing function parameters

Posted: Fri Jul 17, 2009 1:22 am
by Ratzzak
Consider i have a function like this

Code: Select all

 
 function printValues($a,$b="Second",$c="Third") {
 
    echo "$a -- $b -- $c";
}
 
When i call the function, I wanna pass values for the first and third parameters (i.e. for $a and $c) and $b has to take the default value.

Is it possible?

FYI
Sometimes i would wanna pass value for $b and sometime $c... So changing the position of the parameter wont work.

Re: Passing function parameters

Posted: Fri Jul 17, 2009 1:35 am
by Christopher
You can try something like:

Code: Select all

 
 function printValues($a,$b='',$c='') {
    if ($b == '') $b = "Second";
    if ($c == '') $c = "Third";
    echo "$a -- $b -- $c";
}
If you want to be able to pass '' then make the default null.

Re: Passing function parameters

Posted: Fri Jul 17, 2009 2:04 am
by maneetpuri
Hi,

You can do it by function overriding or what you can do is while calling the function pass all the three parameters but keep the value of the one of the parameters blank for which you want the function to use the default value.

Hope this helps.

Cheers,

~Maneet