PHP empty,isset,is_null judgment Comparison (difference and similarities) _php tutorial

Source: Internet
Author: User
Tags parse error
I. Examples and explanations
A. How do we judge a variable without a definition?
Copy CodeThe code is as follows:
#不存在 $test variables

$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 results are:

The result is:Empty,isset first checks whether the variable exists and then detects the value of the variable. And Is_null just checks the value of the variable directly, whether it is null, so if the variable is undefined, an error will occur!

B, look at the respective received parameters are what?

isset function Parameters:

$test = 100;
echo isset ($test), isset (+), $isset ($b =100);



Parse Error: Parse error, unexpected T_lnumber, expecting t_string or t_variable or ' $ ' in PHPDocument3 On line 3

empty function Parameters:

$test = 100;

echo Empty ($test), empty (+), empty ($b =100);



Parse Error: Parse error, unexpected T_lnumber, expecting t_string or t_variable or ' $ ' in PHPDOCUMENT3 on line 3

is_null function Parameters:

$test = 100;

echo Is_null ($test), is_null (+), Is_null ($b =100);

Run Result: no error.

The comparison results come out: theempty,isset input parameter must be a variable (the PHP variable starts with a $ character), and the Is_null input parameter can be as long as it is able to have a return value. (constants, variables, expressions, etc.). In the PHP manual, the parsing for them is that Empty,isset is a language structure and not a function, so it cannot be called by a variable function.

Two, summarize isset,empty,is_null difference:
Just introduced: check variables, and parameter types, this is the 3 functions of the basis of the differences, but also the most easily overlooked. See there are many articles on the Web that compare this 3 function. This is rarely involved. What I'm going to say is that the differences are in the case of checking for existing variables.

Instance:

Copy CodeThe code is as follows:
$a = 100;
$b = "";
$c =null;
Isset Check
echo "Isset", "\ $a = $a", isset ($a)? " 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\r\n";

Empty check
echo "Empty", "\ $a = $a",!empty ($a)? " 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\r\n";

Is_null Check
echo "Is_null", "\ $a = $a",!is_null ($a)? " 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 this simple test, we can generally know that when a variable exists: isset,empty,is_null detection, the value of the situation is obtained. There are no examples of more variables. In fact, the test found:

Empty

If the variable is a non-null or nonzero value, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, Array (), Var $var, undefined; and objects that do not have any properties will be considered empty and return TRUE if Var is empty.

Isset

Returns TRUE if the variable exists (not null), otherwise FALSE (including undefined). The variable value is set to: null, and the variable is canceled after the return is also false;unset a variable. Note that isset for null value variables, special handling.

Is_null

Detects if the passed-in value "value, variable, expression" is null, only one variable is defined, and its value is NULL, it returns TRUE. All others return FALSE "an undefined variable is passed in with an error!" 】.


Question: How do you tell if a variable is set and the value is NULL?

By comparison above, it is estimated that everyone, like me, will have this problem in mind. Detecting whether a variable is null can be used: Is_null, but an error occurs if the variable is undefined with it. Therefore, we think that the detection variable definition can be used: isset, but if a variable value is: null, it will return false. Haha, how to solve this problem? Waiting for everyone to share ...

Check that the variable exists and that the value is null.
Copy CodeThe code is as follows:
function Checknull ($a)
{
if (array_key_exists ($a, $GLOBALS))
{
global $ $a;
if (Is_null ($ $a))
return true;
}
return false;
}
$test =null;
Var_dump (Checknull ("Test"));
Var_dump (Checknull ("test1"));

http://www.bkjia.com/PHPjc/322515.html www.bkjia.com true http://www.bkjia.com/PHPjc/322515.html techarticle First, an example of a . A variable is not defined, how can we judge it? The copy code code is as follows: PHP #不存在 $test variable $isset = isset ($test)? " Test is define! ":" Test I ...

  • Related Article

    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.