-1

I have a function like:

function do_something($first, $second=null, $third=null) { 

    if ($isset($second)) {

        // Do something here

    }

}

Now, I want to pass a value for $third to the function, but I want $second to remain NULL:

do_something('abc','','def');

Now, $second is no longer NULL. How can I pass a value for $third while leaving $second NULL?

4
  • 1
    do_something('abc',null,'def'); Commented Oct 8, 2015 at 16:42
  • do_something('abc', null, 'def'); Commented Oct 8, 2015 at 16:42
  • You mean the second argument still null? Commented Oct 8, 2015 at 16:44
  • Well you could instead use the arguments such $first to signify mandatory and pass an$option = array() and proceed with the logic instead. But basically if u plan on providing that function with a lot of data. Commented Oct 8, 2015 at 16:47

2 Answers 2

4

There is no other way:

do_something('abc', null,'def');
Sign up to request clarification or add additional context in comments.

Comments

2

I think it is quite easy to do this:

do_something('abc', null,'def');

Otherwise, I am linking this SO question which uses array arguments, in order to have optional arguments (credits to sanmai - I'd take no credit for this):

function do_something($arguments = array()) {
// set defaults
    $arguments = array_merge(array(
        "argument" => "default value", 
    ), $arguments); 

    var_dump($arguments);
}

With example usage:

do_something(); // with all defaults, or:
do_something(array("argument" => "other value"));

1 Comment

My favored option, much cleaner, id use extract with that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.