5 ways for PHP to determine whether an array is empty

Source: Internet
Author: User
Tags array empty variables string variable

1. isset function: Determine whether the variable is initialized

Description: It does not determine whether the variable is empty and can be used to determine whether the elements in the array have been defined

Note: When using isset to determine whether an array element is initialized, its efficiency is about 4 times times higher than array_key_exists

 
  
  
  1. <?php
  2. $a = ';
  3. $a [' c '] = ';
  4. if (!isset ($a)) echo ' $a not initialized '. "";
  5. if (!isset ($b)) echo ' $b not initialized '. "";
  6. if (isset ($a [' C '])) echo ' $a has been initialized '. "";
  7. Display results as
  8. $b has not been initialized
  9. $a has been initialized

2. Empty function: Detect whether the variable is "empty"

Description: Any uninitialized variable, a variable with a value of 0 or False or an empty string "" or null, an empty array, an object without any attributes, will be judged as Empty==true

Note 1: Uninitialized variables can also be detected by empty as "null"

Note that 2:empty can only detect variables and not detect statements

 
  
  
  1. <?php
  2. $a = 0;
  3. $b = ';
  4. $c = Array ();
  5. if (Emptyempty ($a)) echo ' $a is empty '. "";
  6. if (Emptyempty ($b)) echo ' $b is empty '. "";
  7. if (Emptyempty ($c)) echo ' $c is empty '. "";
  8. if (Emptyempty ($d)) echo ' $d is empty '. "";

3. var = null function: Determine whether the variable is "null"

Description: A variable with a value of 0 or False or an empty string "" or null, an empty array, will be judged null

Note: The significant difference from empty is that var = null will error when the variable is uninitialized.

 
  
  
  1. <?php
  2. $a = 0;
  3. $b = Array ();
  4. if ($a = = null) echo ' $a is empty '. "";
  5. if ($b = = null) echo ' $b is empty '. "";
  6. if ($c = = null) echo ' $b is empty '. "";
  7. Display results as
  8. $a is empty
  9. $b is empty
  10. Undefined Variable:c

4. Is_null function: Detect whether the variable is "null"

Note: The test result is true when the variable is assigned "null"

Note that 1:null is case-insensitive: $a = null; $a = NULL without any distinction

NOTE 2: Detection results are true,0, empty strings, false, empty arrays are detected as false only if the value of the variable is "null"

Note 3: The program will complain when the variable is not initialized

 
  
  
  1. <?php
  2. $a = null;
  3. $b = false;
  4. if (Is_null ($a)) echo ' $a null '. "";
  5. if (Is_null ($b)) echo ' $b null '. "";
  6. if (Is_null ($c)) echo ' $c null '. "";
  7. Display results as
  8. $a is null
  9. Undefined Variable:c

5. var = = NULL function: Detect whether the variable is "null" and the type of the variable must also be "null"

Note: When the variable is assigned "null" and the type of the variable is also "null", the test result is true

Note 1: On the judgment of "null", all equals the same effect as Is_null

NOTE 2: The program will complain when the variable is not initialized

Summarize:

in PHP, "null" and "NULL" are 2 concepts.

Isset is primarily used to determine whether a variable has been initialized

Empty can judge a value of "false", "empty", "0″," null, "uninitialized" as true

Is_null only evaluates to TRUE for a variable with a value of "null"

var = null evaluates to TRUE for variables with a value of "false", "null", "0″", "null"

var = = null evaluates to true only for variables that have a value of ' null '

Note: When judging whether a variable is really "NULL", most use Is_null to avoid "false", "0″ equivalent interference."



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.