A detailed explanation of the usage differences between empty and isset in PHP

Source: Internet
Author: User
Tags parse error

Isset-detect whether the variable is set
The PHP isset () is used to detect whether one or more variables are set and returns TRUE if the detected variable exists, or FALSE.

Example:

The code is as follows Copy Code
<?php
$var = 1;
if (Isset ($var)) {
Echo ' variable $var has been set ';
} else {
Echo ' variable $var has not yet been set ';
}
?>

Run the example output:

Variable $var has been set

The elements in the array are also valid:

The code is as follows Copy Code

<?php

$a = array (' Test ' => 1, ' hello ' => NULL);

Var_dump (isset ($a [' test ')];//TRUE
Var_dump (isset ($a [' foo ')];//FALSE
Var_dump (isset ($a [' hello '));//FALSE

' Hello ' equals NULL, so it is considered unassigned.
If you want to detect NULL key values, try the bottom method.
Var_dump (array_key_exists (' hello ', $a)); TRUE

Attention

isset () can only be used to detect variables, and passing any other parameter will result in a parse error.
Isset () is a language structure and not a function, so it cannot be invoked by a variable function.

empty-Check if a variable is empty

Empty () returns False if the variable is non-empty or Non-zero

Empty () returns FALSE if Var is a non-null or Non-zero value. In other words, "", 0, "0", NULL, FALSE, Array (), Var $var; and objects that do not have any attributes will be considered empty and TRUE if Var is empty.

Empty () is the antonym of (Boolean) Var, except that no warning is generated when a variable does not have a value.

The code is as follows Copy Code


<?php
$var = 0;

The result is true because the $var is empty
if (empty ($var)) {
Echo ' $var is either 0 or not set at all ';
}

The result is false because the $var has been set
if (!isset ($var)) {
Echo ' $var is isn't set at all ';
}
?>


Example comparison

  code is as follows copy code

$is _var = ' ;

If (isset ($is _var)) {
    echo variable exists! <br/> ";
} else {
    echo variable does not exist! <br/> ";
}

If (empty ($is _var)) {
    echo variable is empty! <br/> ";
} else {
    echo variable is not empty! <br/> ";
}

?

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.