231

I have a question regarding NULL in PHP:

  $a = '';
  if($a == NULL) {
      echo 'is null';
  }

Why do I see is null when $a is an empty string? Is that a bug?

a_hardin
  • 4,991
  • 4
  • 32
  • 40
Erin Tucker
  • 3,274
  • 2
  • 15
  • 22

10 Answers10

383

What you're looking for is:

if($variable === NULL) {...}

Note the ===.
When use ==, as you did, PHP treats NULL, false, 0, the empty string, and empty arrays as equal.

kkurian
  • 3,844
  • 3
  • 30
  • 49
Godwin
  • 9,739
  • 6
  • 40
  • 58
  • 14
    False, your condition only matches uninitialized and null ```$variable```. What you want is actually: ```$variable == null``` (note the ```==```) – Thomas LAURENT Aug 24 '16 at 18:33
  • 9
    @ThomasLAURENT, that's what the OP was looking for, they were wondering why `== null` was matching the empty string and not just null or undefined. – Godwin Aug 24 '16 at 18:51
  • 3
    Oups my mistake, but your last sentence led me think NULL, false, 0 and "" were **strictly** equal which is wrong, sorry for the misunderstanding. – Thomas LAURENT Aug 25 '16 at 21:06
  • I noticed this answer is from 2011. Is this still true in 2016? – James Oct 25 '16 at 19:28
  • 3
    @James it's an intended behaviour in PHP, it's still true even in 2017 and will be true for a long time I suppose – Defrag Feb 23 '17 at 03:09
  • 1
    `$a = ''` is an empty `string`, `false` is a boolean, `$a = 0;` is an integer and `null` is from type `null`. What OP is telling is that PHP will thread them as "same" in value, but not as "same" in type. So a *strict* `===` check will also check for type and fail if you use different ones. That's the reason you should be consistence in your return values. If you normaly return a string in a method like `getName()`, you shouldn't get `null` when it's empty, but more likely an emtpy *string* `$user->getName() === ''` or `$user->getId() === 0` or `$user->isActive === false`. Intended behaviour! – Cagatay Ulubay May 23 '17 at 12:32
  • Beeing inconsistent results in type errors which are the most argument from people that dislike PHP. In facts it's pretty easy.. just don't mix types and be consistent. If you don't know what type you will receive, you check before inserting it (don't let your Entity become invalid by inserting wrong types). Just wanted to add that for people coming back here in future. This probally won't change in the future, like I said, it's intended behaviour and most logical. If you mix them and don't be inconsting.. the real sorcery starts! Like in Javascript (google: javascript nanana batman "wat") – Cagatay Ulubay May 23 '17 at 12:36
  • I've edited the answer's final sentence(s), to avoid the confusion Thomas Laurent had at first. – ToolmakerSteve Nov 21 '19 at 19:46
257

As is shown in the following table, empty($foo) is equivalent to $foo==null and is_null($foo) has the same function of $foo===null. The table also shows some tricky values regarding the null comparison. (ϕ denotes an uninitialized variables. )

         empty    is_null 
         ==null  ===null  isset   array_key_exists
      ϕ |   T   |   T   |   F   |   F   
   null |   T   |   T   |   F   |   T   
     "" |   T   |   F   |   T   |   T   
     [] |   T   |   F   |   T   |   T
      0 |   T   |   F   |   T   |   T      
  false |   T   |   F   |   T   |   T   
   true |   F   |   F   |   T   |   T   
      1 |   F   |   F   |   T   |   T   
     \0 |   F   |   F   |   T   |   T   
Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173
  • 3
    Missing "0" and "0.0" in the table. They make things really tricky, especially empty(). That's why I avoid using empty(). – datasn.io Jun 24 '15 at 04:26
  • 1
    @PHPst Base on the table that you shown, so what is actually the php code of what you called 'simple comparison'? – eQ19 Feb 09 '16 at 12:57
  • @hyip `==null`and `===null`. – Handsome Nerd Feb 09 '16 at 13:00
  • 1
    This should be the top answer, much more complete. Thanks. – pgr Mar 30 '17 at 08:41
  • @PHPst You said that simple comparison is less ambiguous. Can you please provide an example of using simple comparison instead of these NULL empty() etc. – Naeem Ul Wahhab Jan 24 '18 at 19:15
  • isset is the most useful function. when I found out that in php false == null returns true.... I can't even.. that's just plain stupid – hocikto Apr 20 '19 at 09:00
35

check == vs ===

'' == NULL would return true
0 == NULL would return true
false == null would return true

where as

'' === NULL would return false
0 === NULL would return false
false === NULL would return false

jancha
  • 4,916
  • 1
  • 24
  • 39
20

No it's not a bug. Have a look at the Loose comparisons with == table (second table), which shows the result of comparing each value in the first column with the values in the other columns:

    TRUE    FALSE   1       0       -1      "1"     "0"     "-1"    NULL    array() "php"   ""

    [...]    

""  FALSE   TRUE    FALSE   TRUE    FALSE   FALSE   FALSE   FALSE   TRUE    FALSE   FALSE   TRUE

There you can see that an empty string "" compared with false, 0, NULL or "" will yield true.

You might want to use is_null [docs] instead, or strict comparison (third table).

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
13

This is not a bug but PHP normal behavior. It happens because the == operator in PHP doesn't check for type.

'' == null == 0 == false

If you want also to check if the values have the same type, use === instead. To study in deep this difference, please read the official documentation.

Mel
  • 5,837
  • 10
  • 37
  • 42
Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
10

PHP 7 isset() vs empty() vs is_null()

enter image description here

Foued MOUSSI
  • 4,643
  • 3
  • 19
  • 39
9

If you use ==, php treats an empty string or array as null. To make the distinction between null and empty, either use === or is_null. So:

if($a === NULL) or if(is_null($a))

dayuloli
  • 16,205
  • 16
  • 71
  • 126
0

Just to addon if someone is dealing with  , this would work if dealing with  .

Replace it with str_replace() first and check it with empty()

empty(str_replace(" " ,"" , $YOUR_DATA)) ? $YOUR_DATA = '--' : $YOUR_DATA;
Kopi Bryant
  • 1,300
  • 1
  • 15
  • 30
0

NULL stands for a variable without value. To check if a variable is NULL you can either use is_null($var) or the comparison (===) with NULL. Both ways, however, generate a warning if the variable is not defined. Similar to isset($var) and empty($var), which can be used as functions.

var_dump(is_null($var)); // true
var_dump($var === null); // true
var_dump(empty($var)); // true

Read more in How to check if a variable is NULL in PHP?

thomas
  • 785
  • 8
  • 7
-1

Use empty - http://php.net/manual/en/function.empty.php.

Example:

$a = '';
if(empty($a)) {
    echo 'is empty';
}
ewizard
  • 2,801
  • 4
  • 52
  • 110