Viewed   94 times

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?



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.

Saturday, November 19, 2022

is_null($a) is same as $a === null.

($a === null is bit faster than is_null($a) for saving one function call, but it doesn't matter, just choose the style you like.)

For the difference of === and ==, read PHP type comparison tables

$a === null be true only if $a is null.

But for ==, the below also returns true.

null == false
null == 0
null == array()
null == ""
Thursday, November 10, 2022

Consider this script:

$arr = array();
for ($i = 0; $i < 100000; $i++) $arr[] = null;
echo memory_get_usage() . "n";

which on my machine outputs: 21687696, that is 21 MB of used memory. On the other hand using this:

$master_null = null;
$arr = array();
for ($i = 0; $i < 100000; $i++) $arr[] = $master_null;
echo memory_get_usage() . "n";

outputs: 13686832, which is 13 MB. Based on this information, you can assume that at far as memory usage is your concern, it is actually better to indeed use the "master null" variable. However you still need to have all the items in the array, and every entry in a HashTable (internal representation of arrays) takes also some memory.

If you want to dig deeper in the zvals and references, I suggest using the function debug_zval_dump. Using it, you can see, which variables share the same zval:

$a = $b = $c = $d = "abc";
$x = $y = $z = $w = null; 
$q = null;

which outputs:

string(3) "abc" refcount(5)
NULL refcount(5)
NULL refcount(2)

And this implies that although variables $x and $q are both NULL, they are not the same zval. But $x and $y share the same zval, because they are assigned to each other. I believe you know of the function debug_zval_dump, but if not, make sure you carefully read the refcount explanation at

Also at the end of my post, I want to say that this information might be useful for a better knowledge of PHP internals, I think it is quite useless to do any optimizations. Mostly because there are much better places to start optimizing scripts than such micro-optimizations. Also while this is not part of the specification, PHP authors might change this behaviour in the future (e.g. all NULL variables could share the same zval in some future version).

Sunday, December 25, 2022
test="categoryName != ''"

Edit: This covers the most likely interpretation, in my opinion, of "[not] null or empty" as inferred from the question, including it's pseudo-code and my own early experience with XSLT. I.e., "What is the equivalent of the following Java?":

// Equivalent Java, NOT XSLT
!(categoryName == null || categoryName.equals(""))

For more details e.g., distinctly identifying null vs. empty, see johnvey's answer below and/or the XSLT 'fiddle' I've adapted from that answer, which includes the option in Michael Kay's comment as well as the sixth possible interpretation.

Tuesday, September 6, 2022

You can use the IsNullOrEmpty static method:

Sunday, August 21, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :