In actual php applications, the isset () function is generally used for judgment.
Example:
The code is as follows: |
Copy code |
If (isset ($ _ GET ['name']) // checks whether the passed value is null. { Echo "the value is null "; } |
Instructions for use:
1. The values of Null, 0, and false are all set to TRUE by isset.
2. NULL will be judged as FALSE by isset.
3. isset () is also suitable for checking array elements and object elements. If an array or object instance is not defined, false is returned for detecting the array element or object element.
Empty function: checks whether the variable is "null"
For example.
Method 1:
The code is as follows: |
Copy code |
$ SQL = "select * form abc" $ Result = mysql_query ($ SQL ); $ Row = mysql_fetch_array ($ result ); If (empty ($ row) // start to judge whether it is null { Echo "null "; } Else { Echo "not null "; } |
Method 2:
The code is as follows: |
Copy code |
$ SQL = "select * form abc" $ Result = mysql_query ($ SQL ); $ Row = mysql_fetch_array ($ result ); If (! $ Row) // it is null enough to start judgment. { Echo "null "; } Else { Echo "not null "; }
|
In fact, when the mysql_fetch_array function is used, if the table is an empty table, false is returned. $ row is not assigned at this time.
Note: variables and empty arrays whose values are 0, false, or null strings "or" null "are determined to be 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: |
Copy code |
$ 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 value of the variable 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: |
Copy code |
$ 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 |