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". Commented 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'? Commented 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?

Sign up to request clarification or add additional context in comments.

2 Comments

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

i was here for writing that
There's also is_null($b)
is_null($b) too. Tanx @321zeno
So using NULL over FALSE is better for empty default functions?! I updated with my code with the tighter conditional statement ;-)
@tfont yes. Use null instead of false for empty default functions. Also mark your accepted answer
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 Comments

This doesn't really answer the question. But thank you for your method. I suppose that you would suggest using FALSE over NULL then.
yes i do prefer a FALSE over NULL as default value for just one simple reason - False is a value and NULL is not.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.