Passing function parameters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ratzzak
Forum Newbie
Posts: 21
Joined: Sat Jun 13, 2009 10:56 am

Passing function parameters

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Passing function parameters

Post 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.
(#10850)
maneetpuri
Forum Commoner
Posts: 60
Joined: Tue Oct 07, 2008 6:32 am

Re: Passing function parameters

Post 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
Post Reply