PHPempty () isset () is_null () differences and performance comparison

Source: Internet
Author: User
In php, empty () isset () is_null () functions are used to determine whether it is null, however, if I want to gain a detailed understanding of the three functions, there are still many differences between them. is

In php, empty () isset () is_null () functions are used to determine whether it is null, however, if I want to go into details about the three functions, there are still many differences.

Is_null (), empty (), isset (), these functions and = ", = array () are often used in actual operations, because the functions are similar, their differences may be ignored, which may cause a lot of trouble to the work. we will list these structures for your reference. In view of the accuracy of the statements, some explanations are from the original English manual to avoid problems such as untimely updates 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 () and bool are mainly used to determine whether the variable is null. the parameter can only be a variable.

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 () and so on. there are limitations and there is nothing to say. The test type is as follows:

  1. $;
  2. $ B = false;
  3. $ C = '';
  4. $ D = 0;
  5. $ E = null;
  6. $ F = array ();
  7. ?>

Empty ()

The first is the var_dump output of empty:

  1. Var_dump (emptyempty ($ ));
  2. Var_dump (emptyempty ($ B ));
  3. Var_dump (emptyempty ($ c ));
  4. Var_dump (emptyempty ($ d ));
  5. Var_dump (emptyempty ($ e ));
  6. Var_dump (emptyempty ($ f ));
  7. ?>
  8. /*
  9. Program output:
  10. Bool (true)
  11. Bool (true)
  12. Bool (true)
  13. Bool (true)
  14. Bool (true)
  15. Bool (true)
  16. */

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:

  1. Var_dump (isset ($ ));
  2. Var_dump (isset ($ B ));
  3. Var_dump (isset ($ c ));
  4. Var_dump (isset ($ d ));
  5. Var_dump (isset ($ e ));
  6. Var_dump (isset ($ f ));
  7. /* Output
  8. Bool (false)
  9. Bool (true)
  10. Bool (true)
  11. Bool (true)
  12. Bool (false)
  13. Bool (true)
  14. */

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:

  1. Var_dump (is_null ($ ));
  2. Var_dump (is_null ($ B ));
  3. Var_dump (is_null ($ c ));
  4. Var_dump (is_null ($ d ));
  5. Var_dump (is_null ($ e ));
  6. Var_dump (is_null ($ f ));
  7. /* Output
  8. Bool (true)
  9. Bool (false)
  10. Bool (false)
  11. Bool (false)
  12. Bool (true)
  13. Bool (false)
  14. */

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 have seen a lot of articles comparing these three functions on the Internet, which are rarely involved. next I want to talk about the differences in checking all the existing variables. the code is as follows:

  1. $ A = 100;
  2. $ B = "";
  3. $ C = null;
  4. // Isset check
  5. Echo "isset", "$ a = $ a", isset ($ )? "Define": "undefine", "rn ";
  6. Echo "isset", "$ B = $ B", isset ($ B )? "Define": "undefine", "rn ";
  7. Echo "isset", "$ c = $ c", isset ($ c )? "Define": "undefine", "rn ";
  8. Unset ($ B );
  9. Echo "isset", "$ B", isset ($ B )? "Define": "undefine", "rn ";
  10. $ B = 0;
  11. Echo "rnrn ";
  12. // Empty check
  13. Echo "empty", "$ a = $ ",! Emptyempty ($ )? "No empty": "empty", "rn ";
  14. Echo "empty", "$ B = $ B ",! Emptyempty ($ B )? "No empty": "empty", "rn ";
  15. Echo "empty", "$ c = $ c ",! Emptyempty ($ c )? "No empty": "empty", "rn ";
  16. Unset ($ B );
  17. Echo "empty", "$ B ",! Emptyempty ($ B )? "No empty": "empty", "rn ";
  18. $ B = 0;
  19. Echo "rnrn ";
  20. // Is_null check
  21. Echo "is_null", "$ a = $ ",! Is_null ($ )? "No null": "null", "rn ";
  22. Echo "is_null", "$ B = $ B ",! Is_null ($ B )? "No null": "null", "rn ";
  23. Echo "is_null", "$ c = $ c ",! Is_null ($ c )? "No null": "null", "rn ";
  24. Unset ($ B );
  25. Echo "is_null", "$ B", is_null ($ B )? "No null": "null", "rn ";

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.