On Fri, Jan 9, 2009 at 12:44 PM, Wade Preston Shearer <
[email protected]> wrote:

> Consider the following code:
>
> function do_something_awesome($a = 'a', $b = 'b') {}
>
>
> I have set defaults for the variables that the function accepts. I can
> override the defaults, like this:
>
> do_something_awesome('c', 'd');
>
>
> …or…
>
> $a = 'c';
> $b = 'd';
> do_something_awesome($a, $b);
>
>
> And, I can passing in the first while letting the second use the default,
> like this:
>
> do_something_awesome('c');
>
>
> But what if I only want to pass in the second variable? Is that possible?


You can do something like this:

function do_something_awesome() {
    // Assign variables
    if(func_num_args() > 0){
        $a = func_get_arg(0);
        $b = func_get_arg(1);
    }

    // Set empty variables to your defaults
    if (!$a) $a = 'a';
    if (!$b) $b = 'b';

    // Do some stuff
}

That would allow you to send a blank variable that would take on your
default value:

do_something_awesome("", $b);
do_something_awesome($a, "");

_______________________________________________

UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net

Reply via email to