php is null when empty?

PhpNull

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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   

Solution 3 - Php

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

Solution 4 - Php

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).

Solution 5 - Php

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.

Solution 6 - Php

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))

Solution 7 - Php

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

enter image description here

Solution 8 - Php

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;

Solution 9 - Php

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?

Solution 10 - Php

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

Example:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErin TuckerView Question on Stackoverflow
Solution 1 - PhpGodwinView Answer on Stackoverflow
Solution 2 - PhpHandsome NerdView Answer on Stackoverflow
Solution 3 - PhpjanchaView Answer on Stackoverflow
Solution 4 - PhpFelix KlingView Answer on Stackoverflow
Solution 5 - PhpAurelio De RosaView Answer on Stackoverflow
Solution 6 - PhpdayuloliView Answer on Stackoverflow
Solution 7 - PhpFoued MOUSSIView Answer on Stackoverflow
Solution 8 - PhpKopi BryantView Answer on Stackoverflow
Solution 9 - PhpthomasView Answer on Stackoverflow
Solution 10 - PhpewizardView Answer on Stackoverflow