Difference between PHP isset () and empty (), issetempty

Source: Internet
Author: User

Difference between PHP isset () and empty (), issetempty
The isset () function of PHP is generally used to check whether the variable is set.
Format: bool isset (mixed var [, mixed var [,...])

Function: checks whether variables are set.

Return Value:

If the variable does not exist, FALSE is returned.
If the variable exists and its value is NULL, FALSE is returned.
If the variable exists and the value is not NULL, true is returned.
When multiple variables are checked at the same time, TRUE is returned only when each individual item meets the previous requirement; otherwise, the result is FALSE.
Version: PHP 3, PHP 4, PHP 5
More instructions:
When unset () is used to release a variable, it is no longer an isset ().
The PHP function isset () can only be used for variables. Passing any other parameter will cause a parsing error.
Checks whether the defined () function has been set for constants.

PHP's empty () function determines whether the value is null.

Format: bool empty (mixed var)

Function: checks whether a variable is empty.

Return Value:

Returns TRUE if the variable does not exist.
If a variable exists and its values are "", 0, "0", NULL, FALSE, array (), var $ var, and an object without any attribute, true is returned.
If a variable exists and its values are not "", 0, "0", NULL, FALSE, array (), var $ var, or an object without any attribute, FALSE is returned.
Version: PHP 3, PHP 4, PHP 5
More instructions:
Empty () return value =! (Boolean) var, but no warning is generated because the variable is not defined. For more information, see convert to a Boolean value.
Empty () can only be used for variables. Passing any other parameter will cause Paser error and terminate the operation.
Checks whether the defined () function has been set for constants.
Example: A simple comparison between empty () and isset ()
Copy the Code as follows:
<? Php
$ Var = 0;
// The result is true because $ var is null.
If (empty ($ var )){
Echo '$ var is either 0 or not set at all ';
}
// The result is false because $ var has been set.
If (! Isset ($ var )){
Echo '$ var is not set at all ';
}
?>
Note: because this is a language structure rather than a function, it cannot be called by variable functions.
Note: empty () only detects variables, and any non-variables can cause parsing errors. In other words, the following statement does not work: empty (addslashes ($ name )).
The following is the code of a detailed example of isset and empty functions tested by the script house. After reading the code, it is basically the same:
Copy the Code as follows:
<? Php
Error_reporting (E_ALL );
Echo '<B> undefined $ var </B> <Br> ';
Echo "isset test: <Br> ";
If (isset ($ var ))
{
Echo 'variable $ var exists! <Br> ';
}
Echo "empty test: <Br> ";
If (empty ($ var )){
Echo 'variable $ var value is empty <Br> ';
}
Else
{
Echo 'variable $ var value is not empty <Br> ';
}
Echo "Direct Test of variables: <Br> ";
If ($ var ){
Echo 'variable $ var exists! <Br> ';
}
Else {
Echo 'variable $ var does not exist! <Br> ';
}
Echo '---------------------------------- <br> ';
Echo '<B> $ var = \' </B> <Br> ';
Echo "isset test: <Br> ";
$ Var = '';
If (isset ($ var ))
{
Echo 'variable $ var exists! <Br> ';
}
Echo "empty test: <Br> ";
If (empty ($ var )){
Echo 'variable $ var value is empty <Br> ';
}
Else
{
Echo 'variable $ var value is not empty <Br> ';
}
Echo "Direct Test of variables: <Br> ";
If ($ var ){
Echo 'variable $ var exists! <Br> ';
}
Else {
Echo 'variable $ var does not exist! <Br> ';
}
Echo '---------------------------------- <br> ';
Echo '<B> $ var = 0 </B> <Br> ';
Echo 'isset test: <Br> ';
$ Var = 0;
If (isset ($ var ))
{
Echo 'variable $ var exists! <Br> ';
}
Echo "empty test: <Br> ";
If (empty ($ var )){
Echo 'variable $ var value is empty <Br> ';
}
Else
{
Echo 'variable $ var value is not empty <Br> ';
}
Echo "Direct Test of variables: <Br> ";
If ($ var ){
Echo 'variable $ var exists! <Br> ';
}
Else {
Echo 'variable $ var does not exist! <Br> ';
}
Echo '---------------------------------- <br> ';
Echo '<B> $ var = null </B> <Br> ';
Echo 'isset test: <Br> ';
$ Var = null;
If (isset ($ var ))
{
Echo 'variable $ var exists! <Br> ';
}
Echo "empty test: <Br> ";
If (empty ($ var )){
Echo 'variable $ var value is empty <Br> ';
}
Else
{
Echo 'variable $ var value is not empty <Br> ';
}
Echo "Direct Test of variables: <Br> ";
If ($ var ){
Echo 'variable $ var exists! <Br> ';
}
Else {
Echo 'variable $ var does not exist! <Br> ';
}
Echo '---------------------------------- <br> ';

Echo '<B> $ var = "php" </B> <Br> ';
Echo 'isset test: <Br> ';
$ Var = "php ";
If (isset ($ var ))
{
Echo 'variable $ var exists! <Br> ';
}

Echo "empty test: <Br> ";
If (empty ($ var )){
Echo 'variable $ var value is empty <Br> ';
}
Else
{
Echo 'variable $ var value is not empty <Br> ';
}
Echo "Direct Test of variables: <Br> ";
If ($ var ){
Echo 'variable $ var exists! <Br> ';
}
Else {
Echo 'variable $ var does not exist! <Br> ';
}
?>

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 are different: 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:

Copy the Code as follows:
$ 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:

Copy the Code as follows:
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:

Copy the Code as follows:
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)
If var is a non-null or non-zero value, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, array (), var $ var; and objects without any attributes will be considered empty, if var is null, TRUE is returned.
Isset () is described as follows:

Isset () check whether the variable is set

Description: bool isset (mixed var [, mixed var [,...])

If var exists, TRUE is returned; otherwise, FALSE is returned.

If unset () is used to release a variable, it will no longer be isset (). If you use isset () to test a variable that is set to NULL, FALSE is returned. Note that a NULL byte ("?") It is not equivalent to the NULL constant of PHP.
Warning: isset () can only be used for variables, because passing any other parameter will cause a parsing error. To check whether a constant has been set, use the defined () 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.