Php learning path: difference between php empty () and isset (), emptyisset
When using php to write page programs, I often use the variable processing function to determine whether the variable value of a parameter at the end of the php page is null. At the beginning, I used to use the empty () function, but some problems are found. Therefore, the isset () function is used instead.
As the name suggests, empty () determines whether a variable is "null", and isset () determines whether a variable has been set. This so-called "name implies" makes me take some detours at the beginning: When a variable value is equal to 0, empty () will also be True ), as a result, some accidents may occur. Originally, although empty () and isset () are both variable processing functions, they are used to determine whether the variables have been configured, but they have someDifferences:Empty also checks whether the variable is null and zero. When the value of a variable is 0, empty () considers this variable to be null, that is, it is equivalent to not being set.
For example, if the $ id variable is detected and $ id = 0, empty () and isset () are used to check whether the variable $ id has been configured, the two will return different values -- empty () think that there is no configuration, isset () can get the value of $ id:
$ Id = 0;
Empty ($ id )? Print "It's empty.": print "It's $ id .";
// Result: It's empty.
Print "<br> ";
! Isset ($ id )? Print "It's empty.": print "It's $ id .";
// Result: It's 0.
This means that when we use a variable to process a function, when the variable may have a value of 0, we should be careful when using empty (). In this case, it is more wise to replace it with isset.
When the URL tail parameter of a php page shows id = 0 (for example, test. php? Id = 0), try to compare:
If (empty ($ id) $ id = 1;-if id = 0, the id is also 1
If (! Isset ($ id) $ id = 1;-if id = 0, id is not 1
Run the following code separately to detect the above inference:
If (empty ($ id) $ id = 1;
Print $ id; // get 1
If (! Isset ($ id) $ id = 1;
Print $ id; // get 0
To talk about their connection, empty () and isset () are both variable processing functions. They are used to determine whether the variables have been configured, it is because of their great similarity in the Process of processing variables that they lack understanding of their relationships. Considering the functions empty () and isset (), we will confuse people and give them a different angle. The processing objects of empty () and isset () are almost undefined variables, 0, empty strings.
If the variable is 0, empty () returns TRUE, and isset () returns TRUE;
If the variable is a Null String, empty () returns TRUE, and isset () returns TRUE;
If the variable is not defined, empty () returns TRUE, and isset () returns FLASE;
Empty () is described as follows:
Description: bool empty (mixed var)
Empty ()ReturnFALSE. In other words, "", 0, "0 ",NULL,FALSE, Array (), var $ var; And objects without any properties will be considered empty. If var is empty, returnTRUE.
Isset () is described as follows:
Isset () check whether the variable is set
Description: bool isset(Mixed var [, mixed var [,...])
If var exists, returnTRUEOtherwise, returnFALSE.
If you have used unset ()After a variable is released, it is no longer an isset (). If you use isset ()TestNULLWill returnFALSE. Note thatNULLBytes ("\ 0") are not equivalent to PHP'sNULLConstant.
Warning:Isset ()Can only be usedVariableBecause passing any other parameter will cause a parsing error. To detectConstantSet or not, availableDefined ()Function.
You can use the isset function to determine whether a variable has been declared.
You can use the empty function to determine whether a variable has been assigned data and is not empty.
To determine whether a variable exists and is not empty, use the isset function and the empty function.
What is the role of isset and empty in PHP,
Empty
If the variable is not null or a non-zero value, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, array (), var $ var, undefined, and objects without any attributes will be considered empty, if var is null, TRUE is returned.
Isset
If the variable exists (not NULL), TRUE is returned; otherwise, FALSE (including undefined) is returned ). If the value of a variable is set to null, false is returned. If a variable is unset, the variable is canceled. Note that isset has special processing for NULL variables.
Is_null
Checks whether the input value [value, variable, expression] is null. Only if one variable is defined and its value is null, TRUE is returned. otherwise, FALSE is returned. [an error will occur after an undefined variable is passed in! ].
PHP empty, isset, defined and! Between
Empty (): determines whether a variable is null. When a variable exists, checks whether the isset () function creates the variable ,, if a variable does not exist, Judge defined to determine whether to create a constant!