PHP empty () isset () Is_null () Differences and performance comparisons _php tutorials

Source: Internet
Author: User
In PHP empty () isset () Is_null () Three functions are to determine whether it is empty, but if I want to go into the specific depth of the three function found there are still a lot of differences, let me give you a summary.

Is_null (), Empty (), isset (), these functions, and = = ", = = Array () are often used in the actual operation. Because the function is very similar, may neglect their difference, accidentally will cause the work to bring the very big trouble. The following list of these structures for their own reference, in view of the accuracy of the statement, in part to explain the original English manual, to avoid the Chinese manual update is not timely and improper translation and other issues.

Is_null ()
Is_null (), BOOL, Is_null () returns TRUE when the parameter satisfies three cases of NULL.

Null type, the following conditions will be considered null:

It has been assigned the constant NULL.

It has no 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 is not NULL. Parameters can only be variables.

If the variable is not set, or the variable is unset (), or the value of the variable is NULL, returns FALSE, and the other condition returns TRUE. That is, if it is not NULL, it belongs to the Isset category, which is the opposite of the Is_null () function.

If multiple arguments are passed, the intersection is taken. Returns TRUE if all parameters conform to Isset ().

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

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

Empty ()
Empty (), bool, used primarily to determine whether a variable is empty. Parameters can only be variables.

The following conditions will be awarded for locating null:

The code is as follows Copy Code

"" (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 argument is a variable that is not set, the variable will be considered NULL, will not error, and returns TRUE.

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

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

Determine whether the way is empty and = = ", = = Array () and so on, compared with limitations, there is nothing to say.

The types of tests are as follows:

The code is as follows Copy Code

$a;
$b = false;
$c = ";
$d = 0;
$e = null;
$f = Array ();

?>

Empty ()

The first is the empty var_dump output:


:
Var_dump (Empty ($a));
Var_dump (Empty ($b));
Var_dump (Empty ($c));
Var_dump (Empty ($d));
Var_dump (Empty ($e));
Var_dump (Empty ($f));
?>

The program output is:
BOOL (TRUE)
BOOL (TRUE)
BOOL (TRUE)
BOOL (TRUE)
BOOL (TRUE)
BOOL (TRUE)

As you can see from the code, empty () outputs true as long as the data type is empty or false.
Isset ()

And look at the output of Isset:
Var_dump (Isset ($a));
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)

You can see that isset () can only be used to determine if it is null and undefined.
Is_null ()

Finally, the output of the Is_null:
Var_dump (Is_null ($a));
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 literally means it.

This shows that empty () can be used to determine whether all data types are empty or false, and is_null is basically the same as isset, and can only be used to determine if null and undefined.

Summarize Isset,empty,is_null differences:

Just introduced: check variables, and parameter types, this is the 3 functions of the basis of the differences, but also the most easily overlooked. See there are many articles on the Web that compare this 3 function. This is rarely involved. What I'm going to say is that the differences are in the case of checking for existing variables.

The code is as follows Copy Code

$a = 100;

$b = "";

$c =null;

Isset Check

echo "Isset", "$a = $a", isset ($a)? " 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 = $a",!empty ($a)? " 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 = $a",!is_null ($a)? " 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 ";

http://www.bkjia.com/PHPjc/631267.html www.bkjia.com true http://www.bkjia.com/PHPjc/631267.html techarticle in PHP empty () isset () Is_null () Three functions are to determine whether it is empty, but if I want to go into a specific depth to understand the three function found there are still many differences, the following ...

  • 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.