首页 > Web开发 > 详细

关于 PHP Boolean 的细节

时间:2014-07-31 17:16:07      阅读:311      评论:0      收藏:0      [点我收藏+]

最近比较喜欢抠细节,因为这些细节对于代码的编写以及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 . 其实应该是没有太大区别的. 但是始终要记住, 它只是基本的数据类型,并没有什么类的概念...



关于 PHP Boolean 的细节,布布扣,bubuko.com

关于 PHP Boolean 的细节

原文:http://blog.csdn.net/lcstrive/article/details/38315065

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!