PHP Empty,isset,is

Source: Internet
Author: User
Tags define comparison copy empty functions variables parse error
One, the example explains
A. How do we judge a variable without a definition?
Copy CodeThe code is as follows:
<?php
#不存在 $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. The Is_null just checks the value of the variable directly and is null, so if the variable is undefined, an error occurs!

B, to see what the parameters are received?

isset function Parameters:

<?php
$test = 100;
echo isset ($test), Isset (M), $isset ($b =100);

<br/>
<b>parse Error</b>: Parse error, unexpected T_lnumber, expecting t_string or t_variable or "$" in <b>php document3</b> on line <b>3</b><br/>

empty function Parameters:

<?php
$test = 100;

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

<br/>
<b>parse Error</b>: Parse error, unexpected T_lnumber, expecting t_string or t_variable or "$" in <b>php document3</b> on line <b>3</b><br/>

is_null function Parameters:

<?php
$test = 100;

echo Is_null ($test), Is_null (M), Is_null ($b =100);

Run Result: there are no errors.

The comparison came out: the Empty,isset input parameter must be a variable (the PHP variable begins with a $ character), and the Is_null input parameter can be provided with a return value. (constants, variables, expressions, etc.). In the PHP manual, for them parsing is: Empty,isset is a language structure rather than a function, so it cannot be called by the variable function.

Two, Summary summary isset,empty,is_null difference:
Just introduced: check variables, and parameter types, which is the basis for the difference between these 3 functions, and is the most easily overlooked. See there are a lot of comparisons on this 3 functions on the web. These are rarely involved. What I'm going to say here is the difference between checking for variables that already exist.

Instance:

Copy CodeThe code is as follows:
<?php
$a = 100;
$b = "";
$c =null;
Isset Inspection
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 inspection
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 Inspection
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, get the value of the situation. There are no examples of more variables. In fact, the test found:

Empty

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

Isset

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

Is_null

Detects whether the passed-in value "value, variable, expression" is null, has only one variable defined, and its value is NULL, it returns TRUE. All other returns FALSE "undefined variable passed in error!" 】.


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

By comparison, it is estimated that everyone, like me, will have this problem emerging in my mind. Detecting whether a variable is null can be used: is_null, but if the variable is not defined it will be detected with an error. As a result, we think that detecting whether a variable is defined can be used: isset, but it returns false if the value of a variable is: null. 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:
<?php
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"));


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.