PHP Learning Path: The difference between PHP empty () and isset ()

Source: Internet
Author: User

When using PHP to write a page program, I often use the variable handler function to determine the PHP page trailing parameters of a variable value is empty, at the beginning I used to use the empty () function, but found some problems, so instead of using the isset () function, the problem is no longer.

As the name implies, empty () determines whether a variable is "empty" and isset () determines whether a variable has been set. It is this so-called "as the name implies", so I began to take some detours: when a variable value equals 0 o'clock, empty () will also be set (True), so there will be some accidents. Originally, Empty () and isset () are variable-handler functions, which are used to determine whether variables have been configured, but they have a certain difference :empty also detects whether the variable is null and zero. When a variable value of 0,empty () considers the variable equal to NULL, it is equivalent to no setting.

For example, the detection of $id variables, when $id = 0 o'clock, with empty () and isset () to detect whether the variable $id is configured, both will return a different value--empty () think that there is no configuration, isset () can get the value of $id:

$id=0;
Empty($id)?Print "It ' s empty.":Print "It ' s $id.";
//Result: It ' s empty.
    Print "<br>";
!isset($id)?Print "It ' s empty.":Print "It ' s $id.";
//Result: It ' s 0.


This means that when we use the variable handler function, when the variable may appear with a value of 0, use empty () to be careful, it is wiser to replace it with Isset.

When the URL trailing parameter of a PHP page appears id=0 (for example: test.php?id=0), try comparing:

if(Empty($id)) $id=1; -If the ID=0, the ID will also be 1
if(!isset($id)) $id=1; -If the ID=0, the ID will not be 1


The following code can be run separately to detect the above inference:

 if(Empty($id)) $id=1;
Print$id;//Get 1

if(!isset($id)) $id=1;
Print$id;//Get 0



To say their connection, the common point is that empty () and isset () are variable processing functions, the role is to determine whether the variables have been configured, it is because they have a large similarity in the process of variables, it leads to lack of understanding of their relationship. From the two functions of empty () and isset () alone, it would make people more confused and take a different angle. Empty () and isset () deal with no outsidevariable not defined,0,An empty string.
If the variable is 0, empty () returns True,isset () to return true;

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

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

Empty () is explained in the manual as follows:
description bool Empty (mixed Var)
If Var is a non-null or nonzero value, empty ()Return FALSE 。 Other words, "", 0, "0",NULL,FALSE , array (), Var $var; and objects that do not have any properties will be considered empty, and if Var is null, the return TRUE
The Isset () is explained in the manual as follows:
Isset () detects if the variable is set
description bool Isset(mixed var [, mixed Var [, ...]])
returns if Var is present TRUE , or return FALSE
If you have already used unset ()After releasing a variable, it will no longer be isset ()。 If using Isset ()Test one is set to NULL The variable that will return FALSE 。 It is also important to note that a NULL Bytes ("/") is not equivalent to PHP's NULL Constant.
Warning: isset ()can only be usedvariables, because passing any other parameter will cause parsing errors. If you want to detectConstantsis set, you can usedefined ()Function.
You can use the Isset function when you want to determine if a variable has been declared
when you want to determine whether a variable has been given data and is not empty, you can use the empty function
when you want to determine if a variable exists and is not empty first isset function again with the empty function

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.