Empty, isset, is_null comparison (1/4)

Source: Internet
Author: User

Empty, isset, and is_null functions have some problems. It even brings some security bugs to your programs. Most of the time, empty considers isset to be similar. Therefore, during development, I did not pay attention to it. When a piece of code is used as a process judgment, a bug occurs.

 I. Examples

A. a variable is not defined. How should we judge it?

 

View source Print?
01 <?php
02 # Does not exist$test Variable
03   
04 $isset= isset($test)?"test is define!":"test is undefine!";
05 echo "isset:$issetrn";
06   
07 $empty=!empty($test)?"test is define!":"test is undefine!";
08 echo "empty:$emptyrn";
09   
10 $is_null=is_null($test)?"test is define!":"test is undefine!";
11 echo "is_null:$is_nullrn";

 

The test result is:

The result is: empty and isset first checks whether the variable exists and then checks the variable value. Is_null only checks the variable value directly and determines whether it is null. Therefore, if the variable is undefined, an error occurs!

1 2 3 4

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.