When empty($var) != ((bool) $var === false)?

  • 本文关键字:var false bool When empty php
  • 更新时间 :
  • 英文 :


empty() false test和bool类型强制转换之间有什么区别吗?

empty在以下情况下检测错误:

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

答案就在@Bert建议的http://php.net/manual/en/types.comparisons.php

从页上的表派生:

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

最新更新