I am new to the concept of empty and null. Whilst I have endeavoured to understand the difference between them, I am more confused. I came across an article at http://www.tutorialarena.com/blog/php-isset-vs-empty.php however I still don't see when you would use isset and empty when validating forms. Seeing that I don't grasp the difference, I don't want to be using the incorrect functions as well as not be able to use the functions in other areas. Can someone give examples that will help me understand? I am very new to coding so would appreciate if someone could give me real world examples and at the same time keep it simply enough for noob to follow.
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)
$a
is NULL
.
$a = ''
is empty, but not NULL
.
If
$a=''
is empty but notNULL
, when do I use theempty()
function and when do I use theisset()
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.