Several methods of judging variables to be empty in PHP

Source: Internet
Author: User

In summary php, "null" and "NULL" are 2 concepts.

Isset is used primarily to determine if a variable has been initialized.
Empty can evaluate to true for variables with a value of false, NULL, 0, NULL, uninitialized.
Is_null only variables with a value of "null" are judged as true
var = = null evaluates to "false", "null", "0", "null" for variables that are true
var = = NULL only evaluates a variable with a value of "null" to True

So when we judge whether a variable is really "NULL", most use Is_null to avoid "false", "0" equivalent interference.

1. isset function: Determine whether the variable is initialized

Description: It does not determine if the variable is empty and can be used to determine if the element in the array has been defined
Note: When using isset to determine if an array element is initialized, its efficiency is about 4 times times higher than the array_key_exists.

  

Copy the code code as follows:

$a = ";
$a [' c '] = ';
if (!isset ($a)) echo ' $a not initialized '. "";
if (!isset ($b)) echo ' $b not initialized '. "";
if (isset ($a [' C '])) echo ' $a has been initialized '. "";
Show results as
$b has not been initialized
$a has been initialized

  2. Empty function: Detects if the variable is "empty"

Description: Any uninitialized variable, a value of 0 or FALSE, or an empty string "" or a null variable, an empty array, an object without any attributes, will be judged as Empty==true
Note 1: Uninitialized variables can also be detected as "empty" by empty
Note that 2:empty can only detect variables and not detect statements

  

Copy the code code as follows:

$a = 0;
$b = ";
$c = Array ();
if (empty ($a)) echo ' $a is empty '. "";
if (empty ($b)) echo ' $b is empty '. "";
if (empty ($c)) echo ' $c is empty '. "";
if (empty ($d)) echo ' $d is empty '. "";

  3. var = = NULL function: Determine if the variable is "empty"

Description: A value of 0 or False or an empty string "" or a null variable, an empty array, will be judged null
Note: A significant difference from empty is that var = = NULL will be an error when the variable is not initialized.

  

Copy the code code as follows:

$a = 0;
$b = Array ();
if ($a = = null) echo ' $a is empty '. "";
if ($b = = null) echo ' $b is empty '. "";
if ($c = = null) echo ' $b is empty '. "";
Show results as
$a is empty
$b is empty
Undefined Variable:c

  4. Is_null function: Detects if the variable is "null"

Description: The test result is true when the variable is assigned a value of "null"
Note 1:null is case insensitive: $a = null; $a = NULL without any difference
NOTE 2: Test results are true,0, empty strings, false, empty arrays are detected as false only when the value of the variable is "null"
Note 3: The program will error when the variable is not initialized

  

Copy the code code as follows:

$a = null;
$b = false;
if (Is_null ($a)) echo ' $a is null '. "";
if (Is_null ($b)) echo ' $b is null '. "";
if (Is_null ($c)) echo ' $c is null '. "";
Show results as
$a is null
Undefined Variable:c

  5. var = = = NULL Function: Detects if the variable is "null" and the type of the variable must also be "null"

Description: When the variable is assigned a value of "null" and the type of the variable is also "null", the detection result is true
Note 1: On the judgment "null", all equals the same function as Is_null
NOTE 2: The program will error when the variable is not initialized

------------------------------------------------------------------------------------------------

After the value is taken from the database to determine whether it is empty, this looks very simple, as long as the comparison with NULL can be, in fact,

if ($obj ==null) {}

This will cause an error: notice:trying to get the property of non-object problem,

Check it out, you need to use the following notation

if (Isset ($obj)) {echo "This var was set set so I'll print."}

What does this isset do?

The Isset function is to detect whether a variable is set.

Format: bool Isset (mixed var [, mixed Var [, ...])

return value:

Returns FALSE if the variable does not exist
Returns FALSE if the variable exists and its value is null
Returns TURE if the variable exists and the value is not NULL
Returns TRUE if each item meets the previous requirement when checking multiple variables at the same time, otherwise the result is FALSE
If a variable has been freed with unset (), it will no longer be isset (). If you use Isset () to test a variable that is set to NULL, FALSE is returned. Also note that a null byte ("\0″") is not equivalent to PHP's null constant.

Warning: isset () can only be used for variables, because passing any other parameter will result in parsing errors. To detect if a constant is set, use the defined () function.

It seems that the problem with my judgment just now is that this "is a null byte (" \0″) is not equivalent to PHP's null constant ".

Several methods of judging variables to be empty in PHP

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.