PHP里面,有众多的函数检查一个变量是否存在,或者是否为真,或者是否为空。例如: empty, is_null, isSet, == null等等,你知道他们之间的细节差异吗?表中的T就是True, F就是False
原文地址: http://www.blueshoes.org/en/developer/php_cheat_sheet/
检查变量的函数
|
gettype() |
empty() |
is_null() |
isSet() |
(bool) |
$x = “”; |
string |
T |
F |
T |
F |
$x = null; |
NULL |
T |
T |
F |
F |
var $x; (not set) |
NULL |
T |
T |
F |
F |
$x = array(); |
array |
T |
F |
T |
F |
$x = false; |
boolean |
T |
F |
T |
F |
$x = 15; |
integer |
F |
F |
T |
T |
$x = 1; |
integer |
F |
F |
T |
T |
$x = 0; |
integer |
T |
F |
T |
F |
$x = -1; |
integer |
F |
F |
T |
T |
$x = “15″; |
string |
F |
F |
T |
T |
$x = “1″; |
string |
F |
F |
T |
T |
$x = “0″; |
string |
T |
F |
T |
F |
$x = “-1″; |
string |
F |
F |
T |
T |
$x = “foo”; |
string |
F |
F |
T |
T |
$x = “true”; |
string |
F |
F |
T |
T |
$x = “false”; |
string |
F |
F |
T |
T |
注意: empty() 和isSet() 在传入一个未定义变量的时候,报warning的。
用==比较
|
true |
false |
1 |
0 |
-1 |
“1″ |
“0″ |
“-1″ |
“1.3″ |
1.3 |
array() |
“foo” |
“” |
true |
T |
F |
T |
F |
T |
T |
F |
T |
T |
T |
F |
T |
F |
false |
F |
T |
F |
T |
F |
F |
T |
F |
F |
F |
T |
F |
T |
1 |
T |
F |
T |
F |
F |
T |
F |
F |
F |
F |
F |
F |
F |
0 |
F |
T |
F |
T |
F |
F |
T |
F |
F |
F |
F |
T |
T |
-1 |
T |
F |
F |
F |
T |
F |
F |
T |
F |
F |
F |
F |
F |
“1″ |
T |
F |
T |
F |
F |
T |
F |
F |
F |
F |
F |
F |
F |
“0″ |
F |
T |
F |
T |
F |
F |
T |
F |
F |
F |
F |
F |
F |
“-1″ |
T |
F |
F |
F |
T |
F |
F |
T |
F |
F |
F |
F |
F |
“1.3″ |
T |
F |
F |
F |
F |
F |
F |
F |
T |
T |
F |
F |
F |
1.3 |
T |
F |
F |
F |
F |
F |
F |
F |
T |
T |
F |
F |
F |
array() |
F |
T |
F |
F |
F |
F |
F |
F |
F |
F |
T |
F |
F |
“foo” |
T |
F |
F |
T |
F |
F |
F |
F |
F |
F |
F |
T |
F |
“” |
F |
T |
F |
T |
F |
F |
F |
F |
F |
F |
F |
F |
T |
用===比较
|
true |
false |
1 |
0 |
-1 |
“1″ |
“0″ |
“-1″ |
“1.3″ |
1.3 |
array() |
“foo” |
“” |
true |
T |
F |
F |
F |
F |
F |
F |
F |
F |
F |
F |
F |
F |
false |
F |
T |
F |
F |
F |
F |
F |
F |
F |
F |
F |
F |
F |
1 |
F |
F |
T |
F |
F |
F |
F |
F |
F |
F |
F |
F |
F |
0 |
F |
F |
F |
T |
F |
F |
F |
F |
F |
F |
F |
F |
F |
-1 |
F |
F |
F |
F |
T |
F |
F |
F |
F |
F |
F |
F |
F |
“1″ |
F |
F |
F |
F |
F |
T |
F |
F |
F |
F |
F |
F |
F |
“0″ |
F |
F |
F |
F |
F |
F |
T |
F |
F |
F |
F |
F |
F |
“-1″ |
F |
F |
F |
F |
F |
F |
F |
T |
F |
F |
F |
F |
F |
“1.3″ |
F |
F |
F |
F |
F |
F |
F |
F |
T |
F |
F |
F |
F |
1.3 |
F |
F |
F |
F |
F |
F |
F |
F |
F |
T |
F |
F |
F |
array() |
F |
F |
F |
F |
F |
F |
F |
F |
F |
F |
T |
F |
F |
“foo” |
F |
F |
F |
F |
F |
F |
F |
F |
F |
F |
F |
T |
F |
“” |
F |
F |
F |
F |
F |
F |
F |
F |
F |
F |
F |
F |
T |