2016-09-16 87 views
1

空()错误测试和布尔类型转换之间是否有区别?

空探测在下列情况下falsy岬:

"" (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) 

是否有被转换成假的布尔类型转换任何其他值?

+1

是什么问题?不可理解 –

+0

您认为PHP文档中的列表是错误的吗? –

+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)是等价的。