Boolean from MySQL to PHP - Conversion to tinyint and comparing
我的数据库有多个布尔值。保存数据库后,布尔值被转换为tinyint(1)。我认为这是因为它只需要保存1或0。
但是,我现在在比较PHP中的值时遇到了一个问题。我将tinyint保存到数组中,没有进行任何代码转换。该数组有多个文本和日期条目,以及多个带布尔值的条目,例如:
1 2 3 4 5 6 7 |
现在,如果我循环遍历数组,我想检查该值是时间、文本还是真/假。
检查条目是否为真将始终返回真,因为没有条目是空的。检查条目是1还是0对于布尔值有效,但是当我检查"active"=0时,它返回true。为什么会出现这种情况,如果我将字符串与tinyint进行比较,如何才能得到错误的结果?
与==比较在任何情况下都不起作用。
我认为您可以用一些嵌套的if-else语句来实现这一点。但我很确定还有更好的解决方案。:)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | $a=array('09:45:00','10:45:00',1,0,0,1,'active',3.12); foreach ($a as $value) { $type= gettype($value); if ($type =="string") { if(strtotime ($value)){ echo"$value is 'Time' "; } else{ echo"$value is 'String' "; } } elseif ($type =="integer") { if($value == 0 || $value == 1){ echo"$value is 'Boolean' "; } else{ echo"$value is 'Integer' "; } } else{ echo"$value is ($type)!"; } } |