2016-09-16 50 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)是等價的。