empty()falsy-ness test和bool type cast之间有什么区别吗?
在以下情况下,空检测到虚假:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
在bool类型转换中是否还有其他值转换为False?
答案 0 :(得分:0)
答案在http://php.net/manual/en/types.comparisons.php,如@Bert建议
从页面上的表格中获取:
+-----------------------+---------+------------------+-------------------+
| | | | |
+-----------------------+---------+------------------+-------------------+
| Expression | empty() | boolean : if($x) | boolean : if(!$x) |
| $x = ""; | TRUE | FALSE | TRUE |
| $x = null; | TRUE | FALSE | TRUE |
| var $x; | TRUE | FALSE | TRUE |
| $x is undefined | TRUE | FALSE | TRUE |
| $x = array(); | TRUE | FALSE | TRUE |
| $x = array('a', 'b'); | FALSE | TRUE | FALSE |
| $x = false; | TRUE | FALSE | TRUE |
| $x = true; | FALSE | TRUE | FALSE |
| $x = 1; | FALSE | TRUE | FALSE |
| $x = 42; | FALSE | TRUE | FALSE |
| $x = 0; | TRUE | FALSE | TRUE |
| $x = -1; | FALSE | TRUE | FALSE |
| $x = "1"; | FALSE | TRUE | FALSE |
| $x = "0"; | TRUE | FALSE | TRUE |
| $x = "-1"; | FALSE | TRUE | FALSE |
| $x = "php"; | FALSE | TRUE | FALSE |
| $x = "true"; | FALSE | TRUE | FALSE |
| $x = "false"; | FALSE | TRUE | FALSE |
+-----------------------+---------+------------------+-------------------+
这表明empty()
和if(!$x)
是等效的。