## 使用?[===](http://php.net/manual/zh/language.operators.comparison.php)?操作符來檢測 null 和布爾 false 值。
PHP 寬松的類型系統提供了許多不同的方法來檢測一個變量的值。 然而這也造成了很多問題。 使用?`==`?來檢測一個值是否為 null 或 false,如果該值實際上是一個空字符串或 0,也會誤報為 false。?[isset](http://php.net/manual/zh/function.isset.php)?是檢測一個變量是否有值, 而不是檢測該值是否為 null 或 false,因此在這里使用是不恰當的。
[is_null()](http://php.net/manual/zh/function.is-null.php)?函數能準確地檢測一個值是否為 null,?[is_bool](http://php.net/manual/zh/function.is-bool.php)?可以檢測一個值是否是布爾值(比如 false), 但存在一個更好的選擇:`===`?操作符。`===`?檢測兩個值是否同一, 這不同于 PHP 寬松類型世界里的?**相等**。它也比 is_null() 和 is_bool() 要快一些,并且有些人認為這比使用函數來做比較更干凈些。
## 示例
~~~
<?php
$x = 0;
$y = null;
// Is $x null?
if($x == null)
print('Oops! $x is 0, not null!');
// Is $y null?
if(is_null($y))
print('Great, but could be faster.');
if($y === null)
print('Perfect!');
// Does the string abc contain the character a?
if(strpos('abc', 'a'))
// GOTCHA! strpos returns 0, indicating it wishes to return the position of the first character.
// But PHP interpretes 0 as false, so we never reach this print statement!
print('Found it!');
//Solution: use !== (the opposite of ===) to see if strpos() returns 0, or boolean false.
if(strpos('abc', 'a') !== false)
print('Found it for real this time!');
?>
~~~
## 陷阱
* 測試一個返回 0 或布爾 false 的函數的返回值時,如 strpos(),始終使用?`===`?和`!==`,否則你就會碰到問題。
## 進一步閱讀
* [PHP 手冊:比較操作符](http://php.net/manual/zh/language.operators.comparison.php)
* [Stack Overflow: is_null() vs ===](http://stackoverflow.com/questions/8228837/is-nullx-vs-x-null-in-php)
* [Laruence:isset 和 is_null 的不同](http://www.laruence.com/2009/12/09/1180.html)