1

PHP 文档中的类型比较图显示了严格比较===和松散==比较的结果。不等式比较没有任何意义:

true < 0 => false
0 < true => true
true < 1 => false
1 < true => false
true < -1 => false
-1 < true => false

false < 0 => false
0 < false => false
false < 1 => true
1 < false => false
false < -1 => true
-1 < false => false

有人可以合理化上述结果吗?

4

2 回答 2

1

将布尔值转换为整数时,true 将转换为 1,false 将转换为 0

来自 PHP 手册: http: //php.net/manual/en/language.types.integer.php

从布尔值

FALSE 将产生 0(零),TRUE 将产生 1(一)。

还:

-1 被认为是 TRUE,就像任何其他非零(无论是负数还是正数)数字一样!

true  < 0 => false => true < (bool)(0) => true < false => 1 < 0 = false
0 < true => true  => (bool)(0) < true => false < true => 0 < 1 = true
true < 1 => false => true < (bool)(1) => true < true => 1 < 1 = false
1 < true => false => (bool)(1) < true => => true < true => 1 < 1 = false
true < -1 => false => true < (bool)(-1) => true < true => 1 < 1 = false
-1 < true => false => (bool)(-1) < true => true < true = > 1 < 1 = false

false < 0 => false => false < (bool)(0) => false < false => 0 < 0 = false
0 < false => false => (bool)(0) < false => false < false => 0 < 0 = false
false < 1 => true => false < (bool)(1) => false < true => 0 < 1 = true
1 < false => false => (bool)(1) < false => true < false => 1 < 0 = false
false < -1 => true => false < (bool)(-1) => false < true => 0 < 1 = true
-1 < false => false => (bool)(-1) <  false => true < false => 1 < 0 =  false
于 2013-09-03T06:29:59.293 回答
0

真 < 0 => 假 ===> 1 < 0 => 假 (0==0 ==>真)
0 < 真 => 真 ===> 0 < 1 =>
真真 < 1 => 假 == => 1 < 1 => 假 (1==1 ==>真)
1 < 真 => 假 ===> 1 < 1 => 假 (1==1 ==>真)
真 < -1 => false ===> 1 < -1 => false
-1 < true => false ===> -1 < 1 => false ======> 1 < 1 => false(布尔类型被视为无符号)
===> 假 < 0 => 假 ===> 0 < 0 => 假
0 < 假 => 假 ===> 0 < 0 => 假
假 < 1 => 真 ===> 0 < 1 => 真
1 < 假 => 假 ===> 1 < 0 => 假
假 < -1 => 真 ===>0 < -1 => true ======> 0 < 1 => true(布尔类型被视为无符号)
-1 < false => false ===> -1 < 0 => false ======> 1 < 0=> false(布尔类型被视为无符号)

于 2013-09-03T07:13:53.537 回答