What is the difference between null and empty?

A variable is NULL if it has no value, and points to nowhere in memory.

empty() is more a literal meaning of empty, e.g. the string "" is empty, but is not NULL.

The following things are considered to
be empty:

  • “” (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • “0” (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)

Source.


Example

$a is NULL.

$a="" is empty, but not NULL.


Update

If $a="" is empty but not NULL, when do I use the empty() function and when do I use the isset() function.

isset() will return FALSE is the variable is pointing to NULL.

Use empty() when you understand what is empty (look at the list above).

Also when you say it points nowhere in memory, what does that mean exactly?

It means that $str="" will be in memory as a string with length of 0.

If it were $str = NULL, it would not occupy any memory.

Leave a Comment