Php empty, isset, and is_null comparison (Differences and Similarities and Differences)

Source: Internet
Author: User
Tags parse error

I. Examples
A. A variable is not defined. How should we judge it? Copy codeThe Code is as follows: <? Php
# $ Test variable does not exist

$ Isset = isset ($ test )? "Test is define! ":" Test is undefine! ";
Echo "isset: $ isset \ r \ n ";

$ Empty =! Empty ($ test )? "Test is define! ":" Test is undefine! ";
Echo "empty: $ empty \ r \ n ";

$ Is_null = is_null ($ test )? "Test is define! ":" Test is undefine! ";
Echo "is_null: $ is_null \ r \ n ";

The test result is:

The result is: empty and isset first checks whether the variable exists and then checks the variable value. Is_null only checks the variable value directly and determines whether it is null. Therefore, if the variable is undefined, an error occurs!

B. What are the parameters they receive?

Isset function parameters:

<? Php
$ Test = 100;
Echo isset ($ test), isset (100), $ isset ($ B = 100 );

<Br/>
<B> Parse error </B>: parse error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '$' in <B> PHPDocument3 </B> on line <B> 3 </B> <br/>

Empty function parameters:

<? Php
$ Test = 100;

Echo empty ($ test), empty (100), empty ($ B = 100 );

<Br/>
<B> Parse error </B>: parse error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '$' in <B> PHPDocument3 </B> on line <B> 3 </B> <br/>

Is_null function parameters:

<? Php
$ Test = 100;

Echo is_null ($ test), is_null (100), is_null (USD B = 100 );

Running result: no error.

The comparison result is as follows: empty, the isset input parameter must be a variable (the php variable starts with $), and The is_null input parameter can only return values. (Constants, variables, expressions, etc ). In the php manual, the parsing for them is: empty, isset is a language structure rather than a function, so it cannot be called by variable functions.

Ii. Summarize 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 can see many articles comparing these three functions on the Internet. This is rarely involved. Next I want to talk about the differences in checking all existing variables.

Instance:

Copy codeThe Code is as follows: <? Php
$ A = 100;
$ B = "";
$ C = null;
// Isset check
Echo "isset", "\ $ a = $ a", isset ($ )? "Define": "undefine", "\ r \ n ";
Echo "isset", "\ $ B = $ B", isset ($ B )? "Define": "undefine", "\ r \ n ";
Echo "isset", "\ $ c = $ c", isset ($ c )? "Define": "undefine", "\ r \ n ";
Unset ($ B );
Echo "isset", "\ $ B", isset ($ B )? "Define": "undefine", "\ r \ n ";
$ B = 0;
Echo "\ r \ n ";

// Empty check
Echo "empty", "\ $ a = $ ",! Empty ($ )? "No empty": "empty", "\ r \ n ";
Echo "empty", "\ $ B = $ B ",! Empty ($ B )? "No empty": "empty", "\ r \ n ";
Echo "empty", "\ $ c = $ c ",! Empty ($ c )? "No empty": "empty", "\ r \ n ";
Unset ($ B );
Echo "empty", "\ $ B ",! Empty ($ B )? "No empty": "empty", "\ r \ n ";
$ B = 0;
Echo "\ r \ n ";

// Is_null check
Echo "is_null", "\ $ a = $ ",! Is_null ($ )? "No null": "null", "\ r \ n ";
Echo "is_null", "\ $ B = $ B ",! Is_null ($ B )? "No null": "null", "\ r \ n ";
Echo "is_null", "\ $ c = $ c ",! Is_null ($ c )? "No null": "null", "\ r \ n ";
Unset ($ B );
Echo "is_null", "\ $ B", is_null ($ B )? "No null": "null", "\ r \ n ";


Through the above simple test, we can know that when a variable exists: isset, empty, and is_null, the value is obtained. No more variables are provided. In fact, the test found that:

Empty

If the variable is not null or a non-zero value, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, array (), var $ var, undefined, and objects without any attributes will be considered empty, if var is null, TRUE is returned.

Isset

If the variable exists (not NULL), TRUE is returned; otherwise, FALSE (including undefined) is returned ). If the value of a variable is set to null, false is returned. If a variable is unset, the variable is canceled. Note that isset has special processing for NULL variables.

Is_null

Checks whether the input value [value, variable, expression] is null. Only if one variable is defined and its value is null, TRUE is returned. otherwise, FALSE is returned. [an error will occur after an undefined variable is passed in! ].

Q: How can I determine if a variable is set and its value is NULL?

Through the comparison above, it is estimated that, like me, this problem will come to our minds. It can be used to check whether a variable is null: is_null, but if the variable is not defined, it will detect errors. Therefore, we thought whether isset can be used to check whether the variable can be defined. But if the value of a variable is null, it will return false. Haha. How can this problem be solved? Waiting for everyone to share ......

Check whether the variable exists and the value is NULL.Copy codeThe Code is as follows: <? Php
Function checkNull ($)
{
If (array_key_exists ($ a, $ GLOBALS ))
{
Global $;
If (is_null ($ ))
Return true;
}
Return false;
}
$ Test = null;
Var_dump (checkNull ("test "));
Var_dump (checkNull ("test1 "));

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.