Optional Arguments

D’you wanna take this outside?

So you have a function in PHP. It has multiple arguments. In fact, it has multiple optional arguments. It might look something like this:

function options($a=1, $b=2, $c=3){
    echo $a . '-' . $b '-' . $c;

What happens when you want to specify a value for $c but leave $a and $b as their default values?

Short answer: You don’t.

Long answer: No, seriously, it can’t be done. The only way around it is to, instead of passing three variables, pass an array which contains the variables you want to use, then merge this with an array of default values. Or you could pass NULL and then test for it something like this: if ($a = NULL){ $a=1}.


About Mr Chimp

I make music, draw pictures, browse the internet, programme, and make sweet, sweet cups of tea until the early hours.
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

11 Responses to Optional Arguments

  1. Dan says:

    I was under the impression that you could do something like:

    function options($a=NULL, $b=NULL, $c=3){
    echo $a . ‘-‘ . $b ‘-‘ . $c;

    I’m probably wrong though as I have little evidence to back it up.

  2. Mr Chimp says:

    You could do that but it wouldn’t solve the problem.

  3. Skilldrick says:

    how about

    function options($a=1, $b=2, $c=3){
    if(typeof($a) == ‘array’) {
    $newa = $a[‘a’] or 1;
    $newb = $a[‘b’] or $b;
    $newc = $a[‘c’] or $c’
    $a = $newa;
    $b = $newb;
    $c = $newc;
    echo $a . ‘-‘ . $b ‘-‘ . $c;

    I’m not sure about the syntax, but you get the idea…

  4. Skilldrick says:

    Yeah, the ceejayoz one looks pretty good.

    Or is a bit like if not:

    Copying a bit from http://stackoverflow.com/questions/89154/benefits-of-using-short-circuit-evaluation/89313#89313

    open($filename) or die("couldn't open file");
    //is equivalent to:
    if(! open($filename) )
        die("couldn't open file");
    • Skilldrick says:

      So the idea is, if $a[‘a’] is undefined, it’ll be the falsy, and the ‘or’ statement will be execute… I haven’t tried this though :P

      • Mr Chimp says:

        Ah, that’s pretty obvious, really! I’ve seen “or die” before, but never considered that you could use “or” on its own.

      • Skilldrick says:

        Yeah, I came across that recently too. I think it’s short-circuit evaluation, like:

        if( funcThatReturnsTrue() || funcThatBlowsUpTheWorld() ) {
            //let's hope this works!

        Because the first part of the conditional is true, PHP knows that the second part is irrelevant, so doesn’t evaluate it. Phew!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s