您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

关于 PHP Boolean 的细节

2025/12/6 8:00:57发布22次查看
最近比较喜欢抠细节,因为这些细节对于代码的编写以及bug 查找非常有帮助,能够很大程度上提高项目的开发速度.
废话少说,我们开始讲解 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 . 其实应该是没有太大区别的. 但是始终要记住, 它只是基本的数据类型,并没有什么类的概念...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product