2

When you have optional arguments that can have different types, which value is most suited to point out that the argument should not be taken into consideration? False or Null?

Flexo
  • 87,323
  • 22
  • 191
  • 272
Alex
  • 66,732
  • 177
  • 439
  • 641

4 Answers4

6

null is the value used to represent "no value", whereas false means "no", "bad", "unsuccessful", "don't" etc.

Therefore: null.

deceze
  • 510,633
  • 85
  • 743
  • 889
3

For optional arguments use null (generally).

Use null, if you need to differentiate between boolean values (true/false) and nothing (null). On the other hand if you don't need to check for not-set argument and you are using boolean variable then I'd go for false.

Zbigniew
  • 27,184
  • 6
  • 59
  • 66
3

For me this depends on what I'm going to do with the value of said argument...

I am writing a database function where I can have default values as NULL

function somedbfunc($id = NULL, $column1 = NULL)

If these values are null my function may insert a blank record..

If I need to stop my function because of a non argument I may use FALSE

function blah($this = FALSE, $that = FALSE)
{
    if ( ! $this || ! $that)
    {
        return FALSE;
    ....

So I am saying that both are totally valid, but it depends on the situation you find yourself in.

Dale
  • 10,384
  • 21
  • 34
1

As you are saying that you want to tell the "optional arguments" "not be taken into consideration", I will go for null. False is explicitly saying "no" to the recipient. which is a valid input.

billyswong
  • 1,114
  • 7
  • 12