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.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
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 notNULL
.Source.
Example
$a
isNULL
.$a = ''
is empty, but notNULL
.Update
isset()
will returnFALSE
is the variable is pointing toNULL
.Use
empty()
when you understand what is empty (look at the list above).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.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".
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.
The
empty()
is a nice fast way to see if the variable holds any useful info... that is for stringsempty()
returns true for a string of "" as well as a null string.So you can write something like this:
More info see here: PHP: empty()
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 theempty()
function will return true if an int is set to 0. If you need to make sure a variable is exactlyNULL
useif($variable == NULL)
.For more info on
empty()
see http://php.net/manual/en/function.empty.phpThere are some good answers here, which I won't repeat. In the case of validating forms, though, when a form is submitted, the value of each form input element is sent to the server in the
$_POST
variable. You can check for the existence of a particular input by usingisset()
.If this returns true, then this request to the server was the result of posting a form containing an input element named "username". Now that we know that we have a value for that form element, we can see if it has a valid value.
empty()
will tell us whether the user actually entered any data in the field, or whether they left it empty.If that returns true then the form submitted to the server had a field named "username" but the user didn't enter anything into before submitting the form.