2011-04-11 65 views
19

有人可以解释这些语句中null是如何映射的吗?null与PHP < and >运算符

null>0; //=> bool(false) 
null<0; //=> bool(false) 
null==0; //=> bool(true) 

null<-1; // => bool(true) 

我以为这是一些映射问题,但无法破解它。

用Suhosin-Patch试用PHP 5.3.5-1。

+0

呃,你在做什么?所有这些陈述都是有效的。 – Khez 2011-04-11 16:02:25

+1

如果你想要的准确性,使用'==='类型检查相等性 – JohnP 2011-04-11 16:07:40

+0

如果只有PHP的null类似于SQL null ... – 2011-04-11 17:31:37

回答

23

我想指出你几个页面: http://php.net/manual/en/types.comparisons.php http://php.net/manual/en/language.operators.comparison.php http://php.net/manual/en/language.types.boolean.php

因此,在您的最后一个例子:

null<-1 => bool(true) 

null被转换为false-1被转换为truefalse小于true

在你的前两个例子null被转换为false0被转换为falsefalse不小于或大于false更高,但是等于它。

null的乐趣! :D

+0

需要澄清:D,非常好奇为什么null会在bool而不是(int) (int)在<的另一侧 – 2011-04-18 13:25:44