PHP null value detection function and method

Source: Internet
Author: User
This article explains the PHP null value detection function and method. HTTP requests for almost any entry we will detect the parameters it carries, and functions like Isset () empty () must not be uncommon.

The following test results are based on PHP7.16

Whether or not to define judgment: Isset ()

You can use the Isset () function to determine whether a variable is defined, whether an element in an array is initialized, and its efficiency is about 4 times times higher than array_key_exists ().


$a = ' test '; $b = Array (' key1 ' = ' value1 '); $class = new StdClass (); Var_dump (Isset ($class->test->test)); Determines whether an object property is defined: output bool (false) Var_dump (Isset ($a)); Determine if the variable is defined: output bool (true) Var_dump (Isset ($c)); Determine if the variable is defined: output bool (false) Var_dump (Isset ($b [' key1 ']); Determines whether the array element is defined: output bool (true) Var_dump (Isset ($b [' Key2 ']); Determine if an array element is defined: output bool (FALSE)

Is null-judged: empty ()

Empty () function to detect if the variable is null

Any uninitialized amount, 0,false, empty string, null, empty array, using empty to judge will return true


Var_dump (Empty ($c)); output bool (TRUE) var_dump (Empty ($b [' Key2 '])); output bool (TRUE) var_dump (empty ($class)); output bool (false) Var_dump ($class); Output Object (stdClass) #1 (0) {} var_dump (empty ($class->test->test)); output bool (TRUE) class emptyclass{}; $empty _class = new Emptyclass (); Var_dump (Empty ($empty _class));//output bool (false) NULL = = $var Judgment

In addition to judging the undefined variable will be error, and empty judgment, 0,false, empty array, etc. will return true, the essence is = = Expression will be on both sides of the variable automatic type conversion, so the result is true.

Is_null judgment

When a single variable is assigned null or NULL, the result is true, the other condition is false, and if the variable is undefined, an error is given

NULL = = $var judged

Expression: = = = To enforce type detection, not only does it detect the values of variables on both sides of the expression, but also detects the type of the variable, all equal to return true.

0 = = = $var: Detection is 0

false = = = $var: Detect if False

Null = = = $var: detection is null

' = = = $var: Detects if it is an empty string, contains any word nonalphanumeric returns false, such as a space character,

The use of = = is a good distinction between empty strings, 0,false and nulls, and even shaping and floating-point types can be distinguished.


$zero _int = 0; $zero _bool = false; $zero _double = 0.0; $zero _null = null; $zero _str = "; Var_dump (0 = = = $zero _int); output bool (true) var_dump (0 = = = $zero _bool); output bool (false) var_dump (0 = = = $zero _double); output bool (false) var_dump (0 = = = $zero _null); output bool (false) var_dump (0 = = = $zero _str); output bool (false) var_dump ("= = = $zero _str); output bool (TRUE) var_dump (' = = = $zero _int); output bool (false) var_dump ("= = = $zero _bool); output bool (FALSE) var_dump (0.0 = = = $zero _double);//output bool (TRUE) var_dump (0.0 = = = $zero _int); output bool (FALSE)

This article PHP null value detection function and method, more relevant knowledge, please pay attention to the PHP Chinese web.

Related 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.