2

I wrote a simple test:

$test = null;
if ($test == null) print 'is null';
else print 'not null';

This prints out: "is null". Problem is, I changed it to this:

$test = 0;
if ($test == null) print 'is null';
else print 'not null';

And it still prints out null? Why?

rockstardev
  • 13,479
  • 39
  • 164
  • 296
  • 3
    0 is null in PHP: indepth answer here: http://stackoverflow.com/a/17096097/1352271 – JohnnyFaldo Aug 23 '13 at 14:38
  • 4
    Use `===` instead of `==`. – hjpotter92 Aug 23 '13 at 14:39
  • 2
    You are using a loose type unspecific comparison, in that case 0 will equal null. – sg3s Aug 23 '13 at 14:39
  • 1
    You may want to read [type juggling](http://php.net/manual/en/language.types.type-juggling.php) and [type comparison](http://www.php.net/manual/en/types.comparisons.php) – Touki Aug 23 '13 at 14:41
  • Because of == and === are different operators. This will help you: http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – edigu Aug 23 '13 at 14:41

3 Answers3

3

That's because PHP performs a loose comparison when you use ==, i.e. it will attempt to coerce one of the operands into the type of the other if they're not the same.

For example:

array() == false
null == ''
0 == '0'

To compare the value and type together you need the === (triple equals operator), e.g.

if (null === 0) {
    // this can never happen
}

This be the burden of using a dynamically typed language :)

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
2

Use is_null function

is_null($test)

Test

$test = 0;
if (is_null($test)) print 'is null';
else print 'not null';

// Return not null
Bora
  • 10,529
  • 5
  • 43
  • 73
  • Someone explain to me why downvoted? My answer isnt useful? `is_null` function totaly right return! Please leave comment who downvoted users – Bora Aug 23 '13 at 14:53
  • Some claim `is_null` peforms less well over a type specific comparison like `0 === null` but personally I don't think that would be a reason to downvote an answer, even if it's true. – sg3s Aug 23 '13 at 14:57
  • 2
    while you are giving a solution to the question, you are not "answering" it. Question wants to know "HOW" php thinks 0 is null, not how to circumvent that. – Oerd Aug 23 '13 at 14:57
  • @Oerd Answered in comments `0 is null in PHP`. I just give you another solution. Read SO Document http://stackoverflow.com/help/how-to-answer – Bora Aug 23 '13 at 15:02
  • @Bora: 0 is *NOT* null in PHP, it *evaulates* to null in a boolean expression, which is more or less different than being null (depending on who you ask) – Oerd Aug 23 '13 at 15:03
  • `Make sure your answer provides that – or a viable alternative` – Bora Aug 23 '13 at 15:04
  • @Bora: Jack's answer has given it... fwiw, I have not downvoted this answer (yours) I respect you taking the time to write these lines and share your knowledge. There are people who are really tight about this, they are just not the norm in the php community (and it's a pity imho) – Oerd Aug 23 '13 at 16:00
1

PHP treats 0 as NULL.

To check if a value is NULL, you can either use is_null() function or use === in your conditional statement:

$test = 0;
if ($test === null) print 'is null';
else print 'not null';

See a demo here.

Refer to the PHP Manual for more information.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150