Use and difference of empty, isset, and is_null in PHP, issetis_null

Source: Internet
Author: User

Use and difference of empty, isset, and is_null in PHP, issetis_null

1. empty usage

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, returns TRUE if var is null.

2. isset ()

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 ("0") is not equivalent to the NULL constant of PHP.

Note: If the variable does not exist, neither isset () nor empty () will report an error. is_null () and is_numeric () will report an error.

How can we differentiate the following three elements in the array: [0, '', null? (1) Difference 0:

$a = 0;isset($a) && is_numeric($a) === true

(2) Difference''

$a = '';empty($a) && $a=== ''

(3) Difference null

$a = null;is_null($a);  

In addition, when submitting a form, you may often need to check whether a variable exists. For example, $ _ REQUEST ['status'] = 0; empty ($ _ REQUEST ['status']) returns true, but isset ($ _ REQUEST ['status']) is not null.

3. is_null ():

Bool is_null (mixed $ var) (function definition in the official php.net Documentation)
If the following three conditions are met, is_null () returns TRUE. Otherwise, FALSE is returned.
1. It is assigned NULL.
2. It has not been assigned a value.
3. It is not defined. It is equivalent to unset (). After unset () is a variable unset (), isn't it not defined?
Let's look at some examples:

$myvar = NULL;  var_dump(is_null($myvar)); // TRUE $myvar1;      var_dump(is_null($myvar1)); // TRUE Notice: Undefined variable $num = 520; unset($num); var_dump(is_null($num)); //TRUE Notice: Undefined variable var_dump(is_null($some_undefined_var)); //TRUE Notice: Undefined variable  $myvar = 0; is_null($myvar);   // FALSE $myvar = FALSE; is_null($myvar); // FALSE $myvar = ''; is_null($myvar);  // FALSE 

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.