What is the difference between null and empty?

前端 未结 9 1473
一向
一向 2020-12-02 16:32

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.tu

9条回答
  •  無奈伤痛
    2020-12-02 16:52

    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.

提交回复
热议问题