Php judge whether the value is null instance code

Source: Internet
Author: User

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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.