PHPempty () isset () is_null () difference and performance comparison _ PHP Tutorial

Source: Internet
Author: User
PHPempty () isset () is_null () differences and performance comparison. In php, empty () isset () is_null () functions are used to determine whether it is null, however, if I want to gain a deeper understanding of the three functions, there are still many differences. in php, the following section describes empty () isset () is_null () the three functions are used to determine whether they are null. However, if I want to know more about the three functions, there are still many differences, the following is a summary.

Is_null (), empty (), isset (), these functions and = ", = array () are often used in actual operations. Because the functions are similar, they may be ignored, which may cause a lot of trouble for your work. These structures are listed below for your reference. In view of the accuracy of the statement, some explanations are from the original English manual to avoid the problem of untimely updating of the Chinese manual and improper translation.

Is_null ()
Is_null (), bool. if the parameter meets the three conditions of null, is_null () returns TRUE.

Null type, which is considered NULL in the following cases:

It has been assigned the constant NULL.

It has not been set to any value yet.

It has been unset ().

Source: http://cn2.php.net/manual/en/language.types.null.php

Isset ()
Isset (), bool, used to determine whether the parameter is set and not NULL. The parameter can only be a variable.

If no variable is set, the variable is unset (), or the variable value is NULL, FALSE is returned. otherwise, TRUE is returned. That is, if it is not NULL, it belongs to the category of isset. this is the opposite of is_null.

If multiple parameters are passed, the intersection is obtained. That is, TRUE is returned only when all parameters comply with isset.

Ps: defined (), bool, used to check whether a constant is set.

Source: http://cn2.php.net/manual/en/function.isset.php

Empty ()
Empty (), bool, mainly used to judge whether the variable is null. The parameter can only be a variable.

In the following cases, the system will be determined to be empty:

The code is as follows:

"" (An empty string)

0 (0 as an integer)

0.0 (0 as a float)

"0" (0 as a string)

NULL

FALSE

Array () (an empty array)

Var $ var; (a variable declared, but without a value in a class)

Note: If the parameter is an unspecified variable, the variable is considered NULL and no error is reported. TRUE is returned.

But note that after 5.0.0, Objects with no properties are no longer considered empty.

Source: http://cn2.php.net/manual/en/function.empty.php

The method for determining whether it is null is as follows: = ", = array (), etc. there are limitations and there is nothing to say.

The test type is as follows:

The code is as follows:

$;
$ B = false;
$ C = '';
$ D = 0;
$ E = null;
$ F = array ();

?>

Empty ()

The first is the var_dump output of empty:


:
Var_dump (empty ($ ));
Var_dump (empty ($ B ));
Var_dump (empty ($ c ));
Var_dump (empty ($ d ));
Var_dump (empty ($ e ));
Var_dump (empty ($ f ));
?>

Program output:
Bool (true)
Bool (true)
Bool (true)
Bool (true)
Bool (true)
Bool (true)

The code shows that empty () outputs true if the data type is null or false.
Isset ()

Let's take a look at the output of isset:
Var_dump (isset ($ ));
Var_dump (isset ($ B ));
Var_dump (isset ($ c ));
Var_dump (isset ($ d ));
Var_dump (isset ($ e ));
Var_dump (isset ($ f ));

// Output
Bool (false)
Bool (true)
Bool (true)
Bool (true)
Bool (false)
Bool (true)

We can see that isset () can only be used to determine whether it is NULL or not.
Is_null ()

Finally, the output of is_null is as follows:
Var_dump (is_null ($ ));
Var_dump (is_null ($ B ));
Var_dump (is_null ($ c ));
Var_dump (is_null ($ d ));
Var_dump (is_null ($ e ));
Var_dump (is_null ($ f ));

// Output
Bool (true)
Bool (false)
Bool (false)
Bool (false)
Bool (true)
Bool (false)

Is_null.

It can be seen that empty () can be used to determine whether all data types are NULL or false, while is_null is basically the same as isset and can only be used to determine whether it is NULL or undefined.

To sum up the differences among 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": "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 ";

Evaluate () isset () is_null () is used to determine whether it is null, however, if I want to know more about the three functions, there are still many differences...

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.