This article introduces the array problems encountered in PHP Development. the small editor summarizes the methods in section 5 about php's determination of whether the array is empty, for more information about how to determine whether an array is null, see the following section.
This article introduces the array problems encountered in PHP Development. Here we introduce five methods to judge whether the PHP array is empty. if you need it, you can refer to it for reference.
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.
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.
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.
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.
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.
The above is a common PHP method (five methods) for determining whether the array is empty. I hope it will help you. if you have any questions, please leave a message, the editor will reply to you in a timely manner. I would like to thank you for your support for PHP chinnet!
For more articles about common PHP methods to determine whether an array is empty (five methods), refer to PHP!