PHP determines whether the array is empty.

Source: Internet
Author: User

PHP determines whether the array is empty.

1. isset function: determines whether a variable is initialized.

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

Note: When isset is used to determine whether the array element is initialized or not, it is about 4 times more efficient than array_key_exists.

 
 
  1. <? Php
  2. $ A = '';
  3. $ A ['C'] = '';
  4. If (! Isset ($ a) echo '$ a is not initialized '."";
  5. If (! Isset ($ B) echo '$ B is not initialized '."";
  6. If (isset ($ a ['C']) echo '$ a has been initialized '."";
  7. // The result is
  8. // $ B is not initialized
  9. // $ A has been initialized

2. empty function: Check whether the variable is "null"

Note: Any uninitialized variable with a value of 0 or false or an empty string "or a null variable, an empty array, and an object without any attribute, empty = true

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

NOTE 2: empty can only detect variables, but cannot detect statements.

 
 
  1. <? Php
  2. $ A = 0;
  3. $ B = '';
  4. $ C = array ();
  5. If (emptyempty ($ a) echo '$ a is blank '."";
  6. If (emptyempty ($ B) echo '$ B is blank '."";
  7. If (emptyempty ($ c) echo '$ c is blank '."";
  8. If (emptyempty ($ d) echo '$ d is blank '."";

3. var = null: determines whether the variable is "null"

Description: variables and empty arrays whose values are 0, false, or empty 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.

 
 
  1. <? Php
  2. $ A = 0;
  3. $ B = array ();
  4. If ($ a = null) echo '$ a is blank '."";
  5. If ($ B = null) echo '$ B is blank '."";
  6. If ($ c = null) echo '$ B is blank '."";
  7. // The result is
  8. // $ A is empty
  9. // $ B is empty
  10. // 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 detection 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.

 
 
  1. <? Php
  2. $ A = null;
  3. $ B = false;
  4. If (is_null ($ a) echo '$ a is null '."";
  5. If (is_null ($ B) echo '$ B is null '."";
  6. If (is_null ($ c) echo '$ c is null '."";
  7. // The result is
  8. // $ A is NULL
  9. // Undefined variable: c

5. var = null function: checks whether the variable is "null" and the variable type must be "null"

Note: When a variable is assigned "null" and the variable type is "null", the detection result is true.

Note 1: when it is determined to be "null", all functions are the same as is_null.

Note 2: The program reports an error when the variable is not initialized.

Summary:

In PHP, "NULL" and "NULL" are two concepts.

Isset is mainly used to determine whether a variable has been initialized.

Empty can judge TRUE for all variables whose values are "false", "NULL", "0", "NULL", and "uninitialized ".

Is_null only determines the variable with the value of "NULL" as TRUE.

Var = null. All variables with "false", "NULL", "0", and "null" are regarded as TRUE.

Var = null only determines the variable with the value of "NULL" as TRUE.

Note: when determining whether a variable is actually "NULL", is_null is mostly used to avoid interference equivalent to "false" and "0.

 

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.