Usage and difference of empty and isset in PHP

Source: Internet
Author: User
The code is as follows Copy Code

$a = 0;
This time
Empty ($a) = = true;
Isset ($a) = = true;

$a = 1;
This time
Empty ($a) = = false;
Isset ($a) = = true;

Isset ($a) = = True when no $a is defined;
Empty ($a) = = true;

The common denominator is that empty () and isset () are variable processing functions that determine whether variables have been configured, precisely because they have a great similarity in the process of processing variables, leading to a lack of understanding of their relationship. The empty () and isset () of the two functions themselves will make the person more confused and put it in a different angle. The processing objects of empty () and isset () are no more than undefined variables, 0, empty strings.
If the variable is 0, then empty () returns True,isset () to return true;

If the variable is an empty string, then empty () returns True,isset () to return true;

If the variable is undefined, empty () returns True,isset () and returns flase;

Instance

The code is as follows Copy Code


$a = 0;
This time
Var_dump (Empty ($a));/= = true;
Var_dump (Isset ($a));/= = true;


$a = 1;
This time
Var_dump (Empty ($a));//= FALSE;
Var_dump (Isset ($a));/= = true;

When a variable is not defined $bbbbbbbbbb,

Var_dump (Isset ($BBBBBBBBBB));//= FALSE;
Var_dump (Empty ($BBBBBBBBBB));/= = true;

Instance

  code is as follows copy code

<?php

/**
*    isset -detect whether the variable is set
*    returns True if the variable exists, otherwise returns false
*
*    empty -Check whether a variable is empty
*    if the variable is non-null or Non-zero, empty () returns false
**/

$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/> ";
}

?

Empty () determines whether a variable is "empty" and isset () to determine whether a variable has been set. It is this so-called "as the name implies" that I began with some detours: when a variable value is equal to 0 o'clock, empty () will also be set (True), so there will be some surprises. Originally, Empty () and isset (), although they are variable processing functions, are used to determine whether the variables have been configured, they are a certain difference: empty will also detect whether the variable is empty, zero. When a variable value is 0,empty (), the variable equals null, which is equivalent to no setting.

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.