Empty (), is in php

Source: Internet
Author: User
Empty (), is_null (), isset () truth table (difference) let's take a look at the functional descriptions of these three functions www.111cn. netisset determines whether a variable already exists. If the variable exists, TRUE is returned; otherwise, FALSE is returned. Empty determines whether the variable is null. If the variable is not null or a non-zero value, empty () returns FALSE. In other words,

Empty (), is_null (), isset () truth table (difference) let's take a look at the Functional Descriptions of the three functions www.111cn.net isset to determine whether the variable already exists. If the variable exists, TRUE is returned, otherwise, FALSE is returned. Empty determines whether the variable is null. If the variable is not null or a non-zero value, empty () returns FALSE. In other words,

Empty (), is_null (), isset () truth table (difference)

Let's take a look at the Functional Descriptions of the three functions www.111cn.net.

Isset determines whether a variable already exists. If the variable exists, TRUE is returned; otherwise, FALSE is returned.

Empty determines whether the variable is null. If the variable is not null or a 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 the variable is null, TRUE is returned.

Is_null determines whether the variable is NULL

This is the general explanation, but it is confusing. Let's take a look at the specific examples!


From this, we can find that if the variable is "" or 0, or false or null, empty returns true as long as these values are empty.

Isset only determines whether a variable exists. As long as the variable is not null or not assigned a value, the returned result is true. If you use isset () to test a variable that is set to NULL, FALSE is returned. Note that a NULL byte ("") is not equivalent to the NULL constant of PHP.

Is_null is exactly the opposite result of isset. we can regard it! Isset is an inverse operation of isset.

From the above examples, we can draw the following conclusions (which will be frequently used in programming in the future ):

Assume that $ var is of any type.

When empty ($ var) is true, (bool) ($ var) is false. And vice versa.

When is_null ($ var) is true, isset ($ var) is false. And vice versa.

For example:

$ I = $ j + 1;

Here is_null ($ j) is true (it can be understood that because isset ($ j) is false, because $ j is not declared in advance)

Note the following two points:

(1) empty () only detects variables and any non-variables will cause parsing errors. In other words, the following statement does not work: empty (addslashes ($ name )).

(2) 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.


Summary differences between isset, empty, and is_null

As described earlier: Checking variables and parameter types are the basis for the differences between the three functions and are also the most overlooked. I can see many articles comparing these three functions on the Internet. This is rarely involved. Next I want to talk about the differences in checking all existing variables.

The Code is as follows:
$ A = 100;
$ B = "";
$ C = null;
// Isset check
Echo "isset", "$ a = $ a", isset ($ )? "Define": "www.111cN.net undefine", "rn ";
Echo "isset", "$ B = $ B", isset ($ B )? "Define": "undefine", "rn ";
Echo "isset", "$ c = $ c", isset ($ c )? "Define": "undefine", "rn ";
Unset ($ B );
Echo "isset", "$ B", isset ($ B )? "Define": "undefine", "rn ";
$ B = 0;
Echo "rnrn ";

// Empty check
Echo "empty", "$ a = $ ",! Empty ($ )? "No empty": "empty", "rn ";
Echo "empty", "$ B = $ B ",! Empty ($ B )? "No empty": "empty", "rn ";
Echo "empty", "$ c = $ c ",! Empty ($ c )? "No empty": "empty", "rn ";
Unset ($ B );
Echo "empty", "$ B ",! Empty ($ B )? "No empty": "empty", "rn ";
$ B = 0;
Echo "rnrn ";

// Is_null check
Echo "is_null", "$ a = $ ",! Is_null ($ )? "No null": "null", "rn ";
Echo "is_null", "$ B = $ B ",! Is_null ($ B )? "No null": "null", "rn ";
Echo "is_null", "$ c = $ c ",! Is_null ($ c )? "No null": "null", "rn ";
Unset ($ B );
Echo "is_null", "$ B", is_null ($ B )? "No null": "null", "rn ";



Through the above simple test, we can know that when a variable exists: isset, empty, and is_null, the value is obtained. No more variables are provided. In fact, the test found that:

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! ].

For more details, see: http://www.111cn.net/phper/php/56681.htm

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.