Jump to content

Booleans: What is the difference between NULL and empty?


eldan88

Recommended Posts

Hi,

 

I  am pretty sure this questions gets asked a lot. I would like to know what is the difference between those two. I know they share similarities. But from what I know a 0 integer or a 0 string is considered empty.  How do I know when to use NULL and When to use empty ? Thanks!

Link to comment
Share on other sites

It all depends on what you are doing:

$a = "";

$b = 0;

$c = NULL;

$d = "0";

 

function            $a        $b        $c        $d

is_numeric          false    true      false    true

is_null            false    false    true      false

is_string          true      false    false    true

empty              true      true      true      true

 

I've used it for multipurpose functions before:

function multiPurpose( $value=NULL ) {
  if( is_null( $value ) ) {
    // this means no value was sent
  }
  elseif( is_numeric( $value ) ) {
    // this means a number was sent
  }
  elseif( is_array( $value ) ) {
    // this means an array was sent
  }
  else {
    // if it got here, it is none of the above, probably text
  }
}

And sometimes it doesn't really matter.

Link to comment
Share on other sites

in my opinion

 

the NULL is not assign anything to the variable

and

most of default value are EMPTY value

so if check it by empty() it return TRUE

but if check by is_null()  it should be return FALSE

 

$a = NULL; // i think $a will not keep anything
$b;  // it assign to the default value --> i think it must be the empty value

Link to comment
Share on other sites

$a = NULL; // i think $a will not keep anything
$b;  // it assign to the default value --> i think it must be the empty value

Actually, both is_null( $a ) and is_null( $b ) will return TRUE responses. An unset or uninitialized variable is considered to have a NULL value. Setting a value to be NULL will also return a FALSE with the isset function, even though the value is set. Let's expand the chart:

$a = "";

$b = 0;

$c = NULL;

$d = "0";

unset( $e );

 

function            $a        $b        $c        $d        $e

is_numeric          false    true      false    true      false

is_null            false    false    true      false    true

is_string          true      false    false    true      false

empty              true      true      true      true      true

isset              true      true      false    true      false

==""                true      true      true      false    true

===""              true      false    false    false    false

Link to comment
Share on other sites

Hi,

 

I  am pretty sure this questions gets asked a lot. I would like to know what is the difference between those two. I know they share similarities. But from what I know a 0 integer or a 0 string is considered empty.  How do I know when to use NULL and When to use empty ? Thanks!

 

I would recommend reading the PHP manual, on the "type comparison" page: http://www.php.net/manual/en/types.comparisons.php

 

in my opinion

 

the NULL is not assign anything to the variable

and

most of default value are EMPTY value

so if check it by empty() it return TRUE

but if check by is_null()  it should be return FALSE

 

$a = NULL; // i think $a will not keep anything
$b;  // it assign to the default value --> i think it must be the empty value

 

$a 'exists' but is set to NULL; isset and is_null will return FALSE and TRUE, respectively.

 

NULL is not really a value, but the variable exists. For example:

$ php -r 'if(is_null($b)) { echo("\$b is null\n"); }'
PHP Notice:  Undefined variable: b in Command line code on line 1

Notice: Undefined variable: b in Command line code on line 1
$b is null

 

$b, in this case, was undefined, and something that isn't set is "NULL" in a way, but since it isn't set, it's going to trigger an error. If $b had be assigned NULL in a previous expression, there would be no warning triggered.

 

Furthermore, there is no default value for a variable, so one cannot define a variable like so:

$b;

Without assigning it to anything. It will trigger an error if $b is referenced by an expression.

 

$ php -r '$b; echo($b);'
PHP Notice:  Undefined variable: b in Command line code on line 1

Notice: Undefined variable: b in Command line code on line 1

 

Anyways, the tables on the page I linked have a very good explanation of how these things all map to each other.

 

Cheers.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.