php is null or empty?

Erin Tucker picture Erin Tucker · Nov 23, 2011 · Viewed 425.6k times · Source

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?

Answer

Godwin picture Godwin · Nov 23, 2011

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.