PHP 5 ways to determine if an array is empty
Http://www.php100.com/html/it/biancheng/2015/0422/8925.html
Source: Code Farm time: 2015-04-22 13:51:11 read:18914
share to: 6
[Introduction] This article introduced the PHP development encountered in the array problem, here is to judge the PHP array is empty 5 ways, the need for friends can reference.
This article introduced the PHP development encountered in the array problem, here is to judge the PHP array is empty 5 methods, the need for friends can learn from reference.
1. isset function: Determine whether the variable is initialized
Description: It does not determine if the variable is empty and can be used to determine if the element in the array has been defined
Note: When using isset to determine if an array element is initialized, its efficiency is about 4 times times higher than the array_key_exists.
View Source print?
01.
<?php
02.
$a
=
‘‘
;
03.
$a
[
‘c‘
] =
‘‘
;
04.
if
(!isset(
$a
))
echo
‘$a 未被初始化‘
.
""
;
05.
if
(!isset(
$b
))
echo
‘$b 未被初始化‘
.
""
;
06.
if
(isset(
$a
[
‘c‘
]))
echo
‘$a 已经被初始化‘
.
""
;
07.
// 显示结果为
08.
// $b 未被初始化
09.
// $a 已经被初始化
2. Empty function: Detects if the variable is "empty"
Description: Any uninitialized variable, a value of 0 or FALSE, or an empty string "" or a null variable, an empty array, an object without any attributes, will be judged as Empty==true
Note 1: Uninitialized variables can also be detected as "empty" by empty
Note that 2:empty can only detect variables and not detect statements
View Source print?
1.
<?php
2.
$a
= 0;
3.
$b
=
‘‘
;
4.
$c
=
array
();
5.
if
(
empty
empty
(
$a
))
echo
‘$a 为空‘
.
""
;
6.
if
(
empty
empty
(
$b
))
echo
‘$b 为空‘
.
""
;
7.
if
(
empty
empty
(
$c
))
echo
‘$c 为空‘
.
""
;
8.
if
(
empty
empty
(
$d
))
echo
‘$d 为空‘
.
""
;
3. var = = NULL function: Determine if the variable is "empty"
Description: A value of 0 or False or an empty string "" or a null variable, an empty array, will be judged null
Note: A significant difference from empty is that var = = NULL will be an error when the variable is not initialized.
View Source print?
01.
<?php
02.
$a
= 0;
03.
$b
=
array
();
04.
if
(
$a
== null)
echo
‘$a 为空‘
.
""
;
05.
if
(
$b
== null)
echo
‘$b 为空‘
.
""
;
06.
if
(
$c
== null)
echo
‘$b 为空‘
.
""
;
07.
// 显示结果为
08.
// $a 为空
09.
// $b 为空
10.
// Undefined variable: c
4. Is_null function: Detects if the variable is "null"
Description: The test result is true when the variable is assigned a value of "null"
Note 1:null is case insensitive: $a = null; $a = NULL without any difference
NOTE 2: Test results are true,0, empty strings, false, empty arrays are detected as false only when the value of the variable is "null"
Note 3: The program will error when the variable is not initialized
View Source print?
01.
<?php
02.
$a
= null;
03.
$b
= false;
04.
if
(
is_null
(
$a
))
echo
‘$a 为NULL‘
.
""
;
05.
if
(
is_null
(
$b
))
echo
‘$b 为NULL‘
.
""
;
06.
if
(
is_null
(
$c
))
echo
‘$c 为NULL‘
.
""
;
07.
// 显示结果为
08.
// $a 为NULL
09.
// Undefined variable: c
5. var = = = NULL Function: Detects if the variable is "null" and the type of the variable must also be "null"
Description: When the variable is assigned a value of "null" and the type of the variable is also "null", the detection result is true
Note 1: On the judgment "null", all equals the same function as Is_null
NOTE 2: The program will error when the variable is not initialized
Summarize:
in PHP, "null" and "NULL" are 2 concepts.
Isset is used primarily to determine if a variable has been initialized.
Empty can evaluate to true for variables with a value of false, NULL, 0″, NULL, uninitialized
Is_null only variables with a value of "null" are judged as true
var = = null evaluates to "false", "null", "0″", "null" for variables that are true
var = = NULL only evaluates a variable with a value of "null" to True
Note: When judging whether a variable is truly "NULL", most use Is_null to avoid "false", "0″ equivalent interference."
PHP 5 ways to determine if an array is empty