-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?

MultiDev
  • 10,389
  • 24
  • 81
  • 148

2 Answers2

4

There is no other way:

do_something('abc', null,'def');
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
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"));
Community
  • 1
  • 1
codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37