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. for example: if (isset ($ _ GET [#39; name #39;]) judge whether the passed value is null .... In actual php applications, the isset () function is generally used for judgment. The example is as follows:

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 applicable to the check of array elements and object elements. If an array or object instance is not defined, false is returned for the check of array elements/object elements.

Empty function: checks whether the variable is "null"

For example, the code is as follows:

$ SQL = "select * form abc" $ result = mysql_query ($ SQL); $ row = mysql_fetch_array ($ result); if (emptyempty ($ row )) // start to judge whether it is null. {echo "null";} else {echo "not null ";}

Method 2: The code is as follows:

$ SQL = "select * form abc" $ result = mysql_query ($ SQL); $ row = mysql_fetch_array ($ result); if (! $ Row) // start to judge whether it is null. {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 with a value of 0 or false or an empty string "" or null, an empty array, will all be judged as null.

Note:A significant difference from empty is that when the variable is not initialized, var = null will report an error.

The PHP instance 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 displayed result is // $ a is blank // $ B is blank // 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 PHP instance 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 displayed result is // $ a is NULL // Undefined variable: c


Article address:

Reprint at will ^ please include the address of this article!

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.