废话少说,我们开始讲解 php boolean.
首先,boolean 型是 php的基本数据类型, 它要么是true,或者false. 大小写不区分.
如果需要将其他类型转换为bool类型的时候, 可以使用(bool) 或者(boolean),(boolean)进行转换.
如
echo true === (bool)-1;echo true === (boolean)-1;echo true === (boolean)-1;
public function test_true () { $this->asserttrue(-1 == true); //-1 => true $this->asserttrue(false == true); //false => true $this->asserttrue(null == true); //null => true $this->asserttrue(0.0 == true); //0.0 => true $this->asserttrue((bool)-1); $this->asserttrue((boolean)-1); $this->asserttrue((boolean)-1); } public function test_false () { $this->asserttrue (null == false);//null => false $this->asserttrue (0 == false); //0 => false $this->asserttrue (0.0 == false); //0.0 => false $this->asserttrue ( == false); // => false $this->assertfalse ( == false); // => true }
以上是我写的两个简单的测试用例. 全部都断言成功.
-1 == true 结果为true.
同理 null == false 结果也为true .
就是非严格的类型比对.
还有要注意搞清楚 bool , boolean , boolean . 其实应该是没有太大区别的. 但是始终要记住, 它只是基本的数据类型,并没有什么类的概念...
