What is the difference between null and empty?

前端 未结 9 1478
一向
一向 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: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.

提交回复
热议问题