Several methods for determining null variables in PHP

Source: Internet
Author: User
Tags set set
Several methods for determining null variables in PHP 1. isset function: determines whether a variable is initialized.

Note: it does not determine whether the variable is null and can be used to determine whether the elements in the array have been defined.
Note: When isset is used to determine whether the array element is initialized or not, it is about 4 times more efficient than array_key_exists.

  

The code is as follows:

$ A = '';
$ A ['c'] = '';
If (! Isset ($ a) echo '$ a is not initialized '."";
If (! Isset ($ B) echo '$ B is not initialized '."";
If (isset ($ a ['c']) echo '$ a has been initialized '."";
// The result is
// $ B is not initialized
// $ A has been initialized

  2. empty function: Check whether the variable is "null"

Note: any uninitialized variable, variable with a value of 0 or false or null string "or null, empty array, and object without any attribute, empty = true
Note 1: uninitialized variables can also be detected as "null" by empty"
Note 2: empty can only detect variables, but cannot detect statements.

  

The code is as follows:

$ A = 0;
$ B = '';
$ C = array ();
If (empty ($ a) echo '$ a is blank '."";
If (empty ($ B) echo '$ B is blank '."";
If (empty ($ c) echo '$ c is blank '."";
If (empty ($ d) echo '$ d is blank '."";

  3. var = null: determines whether the variable is "null"

Note: variables and empty arrays whose values are 0, false, or empty strings "" or null will all be considered null.
Note: a significant difference from empty is that when the variable is not initialized, var = null will report an error.

  

The code is as follows:

$ A = 0;
$ B = array ();
If ($ a = null) echo '$ a is blank '."";
If ($ B = null) echo '$ B is blank '."";
If ($ c = null) echo '$ B is blank '."";
// The result is
// $ A is empty
// $ B is empty
// Undefined variable: c

  4. is_null function: checks whether the variable is "null"

Note: when the variable is assigned "null", the detection result is true.
Note 1: null is case insensitive: $ a = null; $ a = NULL no difference
Note 2: The check result is true only when the variable value is "null". the values 0, null string, false, and empty array are both false.
Note 3: The program reports an error when the variable is not initialized.

  

The code is 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 '."";
// The result is
// $ A is NULL
// Undefined variable: c

  5. var = null: Check whether the variable is "null" and the variable type must be "null"

Note: When a variable is assigned "null" and the variable type is "null", the detection result is true.
Note 1: When it is determined to be "null", all functions are the same as is_null.
Note 2: The program reports an error when the variable is not initialized.

  In summary, "NULL" and "NULL" are two concepts in PHP.

Isset is mainly used to determine whether a variable has been initialized.
Empty can be used to set TRUE values for "false", "NULL", "0", "NULL", and "uninitialized" variables.
Is_null only determines the variable whose value is "NULL" as TRUE.
Var = null. all variables with false values, empty values, 0 values, and NULL values are regarded as TRUE.
Var = null only determines the variable with the value of "NULL" as TRUE.

Therefore, when determining whether a variable is "NULL", is_null is mostly used to avoid interference equivalent to "false" and "0.


Bytes ------------------------------------------------------------------------------------------------


If it is null after the value is retrieved from the database, it looks very simple. you only need to compare it with null. In fact, it is not,

if($obj==null){ }

In this case, the following error occurs: Notice: Trying to get property of non-object problem,

I checked and found that the following statement is required:

if (isset($obj)) { echo "This var is set set so I will print."; }

What is this isset?

The isset function checks whether variables are set.

Format: bool isset (mixed var [, mixed var [,...])

Return value:

If the variable does not exist, FALSE is returned.
If the variable exists and its value is NULL, FALSE is returned.
If the variable exists and the value is not NULL, true is returned.
When multiple variables are checked at the same time, TRUE is returned only when each individual item meets the previous requirement; otherwise, the result is FALSE.
If unset () is used to release a variable, it will no longer be isset (). If you use isset () to test a variable that is set to NULL, FALSE is returned. Note that a NULL byte ("\ 0") is not equivalent to the NULL constant of PHP.

Warning: isset () can only be used for variables, because passing any other parameter will cause a parsing error. To check whether a constant has been set, use the defined () function.

It seems that the problem with my judgment just now is that "it is a NULL byte (" \ 0 ") and not equivalent to the NULL constant of 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.