What is the difference between null and empty?

前端 未结 9 1434
一向
一向 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条回答
  • The table below is an easy reference for what these functions will return for different values. The blank spaces means the function returns bool(false). enter image description here

    refer this link for more https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

    0 讨论(0)
  • 2020-12-02 16:39

    isset() returns true if both these conditions are met:

    1. The variable has been defined and has not yet been unset.
    2. The variable has a non-null value in it.

    A variable is automatically defined when it gets set to something (including null). This has a direct implication in arrays.

    $a=array();
    $a['randomKey']=true;
    $a['nullKey']=null;
    var_dump(isset($a['randomKey'])); // true
    var_dump(isset($a['nullKey'])); // true, the key has been set, set to null!
    var_dump(isset($a['unsetKey'])); // false !
    unset($a['randomKey']);
    var_dump(isset($a['randomKey'])); // false ! it's been unset!
    

    From above, you can check if various $_POST fields have been set. For example, a page that has been posted to, stands to reason, has the submit button name in the $_POST field.

    empty() on the other hand, tests if the variable holds a non zero value. This means that values that (int) cast to 0, return false too. You can use this to see if a specific $_POST field has data in it.

    0 讨论(0)
  • 2020-12-02 16:43

    NULL is a special value which explicitly states that the variable has not been set to any value yet. Be careful with using the empty() function as you can't just determine that a variable is exactly NULL using it. For example the empty() function will return true if an int is set to 0. If you need to make sure a variable is exactly NULL use if($variable == NULL).

    For more info on empty() see http://php.net/manual/en/function.empty.php

    0 讨论(0)
  • 2020-12-02 16:46

    This concept can be better understood from mathematics. Have you ever tried dividing a number (not zero) by 0 using a calculator e.g 7/0? You will get a result that looks like something this: undefined, not a number, null etc. This means that the operation is impossible, for some reasons (let's leave those reasons to be discussed another day).

    Now, perform this: 0/7. You will get the output, 0. This means that the operation is possible and can be executed, but you the answer is just 0 because nothing is left after the division. There is a valid output and that output is zero.

    In the first example, not only was the output invalid, the operation was not possible to execute. This is akin to null. The second example is akin to empty.

    0 讨论(0)
  • 2020-12-02 16:50

    Been awhile since i used PHP but if other languages are anything to go by empty will indicate an existing object/map/array that has no contents while null would indicate a variable that has no meaning/definition at all (uninitialised).

    In database SQL, NULL means "no value".

    0 讨论(0)
  • 2020-12-02 16:51

    Null is a placeholder that generally means "no data about this is available".

    The use of null for this is just a convention, but a rather widespread one, to the point where some programming languages support the convention directly. The reason this convention exists has IMHO historically to do with "pointers"; many times a procedure will be defined to return a pointer to an answer, and will return what is traditionally called a Null pointer if it could not produce an answer for some reason.

    Empty means (if this is a set) that it has no members. That's an explicit answer, and it is very different than "no data about this is available".

    In the PHP world, apparantly uninitialized variables have the Null value, and isset on such a variable returns FALSE. For arrays and strings, PHP follows the convention that "empty" means "has no members" although arrays and strings are not technically sets.

    PHP apparantly has this funny idea that 0 and 0.0 are also "empty", by PHP design. That's abusive of the concept of "empty" IMHO: Individual numbers are not sets, so 0 can't reasonably by "empty". THis just leads to obscure programming because it violates the principle of least surprise. I'm sure the PHP designers would are that "zero is the empty number" as some kind of vague analogy; but the if analogy is vague, why bother with it? But then PHP is full of silly ideas.

    0 讨论(0)
提交回复
热议问题