0

Should empty function arguments be set to NULL or FALSE?

As example follows:

function test1($a = FALSE, $b = FALSE)
{
    if ($b)
    {
        // ... (some awesome code here)
    }

    //.. (more awesome code)
}

test1(0);

OR

function test2($a = NULL, $b = NULL)
{
    if ($b !== NULL)
    {
        // ... (some awesome code here)
    }

    //.. (more awesome code)
}

test2(0);

Note; Also something to consider - while using $a === NULL. One could also use '!empty()' depending if the below code requires empty values or not.

Which is a better design and why?

2
  • 2
    Option 3 might be the best. Have separate functions. But with the provided info it's pretty much impossible to say which one of the three is "the better design".
    – PeeHaa
    Jan 24, 2016 at 14:31
  • Do you have 3 cases or just 2? I.e., are you going to do something different if '$a == false' versus 'isset( $a ) == false'?
    – marklark
    Jan 24, 2016 at 15:21

3 Answers 3

2

Logically a NULL value should treat as something that doesn't exist. FALSE instead has some semantic value, even if you can consider it as an empty value.

So, use FALSE for an answer to yes or no, NULL as no answer. If your argument is an option like $enable_this it can be a boolean value, and you may want check if it's true with

if ($var)

Instead if you can optionally pass something as an argument, by default it can be NULL, and then you can check if it's really there with

if($var !== null)

You should use empty only if you want test it to empty value. Cannot $var be an empty string, an empty array, "0" or 0?

2
  • So you suggest if the optional parameter is a boolean then it should be FALSE/TRUE and if it's integer, string, array, or object then it should be NULL ?
    – tfont
    Jan 25, 2016 at 12:08
  • 1
    That depends on what you're doing. There are some case when you want use NULL by default even when the parameter can be a boolean. Use NULL as a there-is-nothing-here.
    – Federkun
    Jan 25, 2016 at 12:38
2

Use this

function test($a = NULL, $b = NULL)
{
    if ($b !== NULL)
    {
        // ... (some awesome code here)
    }

    //.. (more awesome code)
}

test(0);

Dont use if(!empty($b)), if($b!=NULL) and if(!is_null($b)) because $b=false && $b=0 will fail in this case

5
  • i was here for writing that
    – synan54
    Jan 24, 2016 at 14:39
  • There's also is_null($b)
    – 321zeno
    Jan 24, 2016 at 14:43
  • 1
    is_null($b) too. Tanx @321zeno Jan 24, 2016 at 14:44
  • So using NULL over FALSE is better for empty default functions?! I updated with my code with the tighter conditional statement ;-)
    – tfont
    Jan 25, 2016 at 12:03
  • 1
    @tfont yes. Use null instead of false for empty default functions. Also mark your accepted answer Jan 25, 2016 at 12:33
0

You could also do it like this:

function test1($a = FALSE, $b = FALSE) {
  if ($b===false) {
    // ... (process default val-false)
  }else{
    //.. (process supplied value)
  }
}
2
  • This doesn't really answer the question. But thank you for your method. I suppose that you would suggest using FALSE over NULL then.
    – tfont
    Jan 25, 2016 at 12:06
  • yes i do prefer a FALSE over NULL as default value for just one simple reason - False is a value and NULL is not.
    – Ranhot
    Jan 27, 2016 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.