【问题标题】:null with PHP < and > operators带有 PHP < 和 > 运算符的 null
【发布时间】:2011-04-11 15:57:20
【问题描述】:

有人能解释一下这些语句中的 null 是如何映射的吗?

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

但是

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

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

尝试使用带有 Suhosin-Patch 的 PHP 5.3.5-1。

【问题讨论】:

  • 呃,你想做什么?所有这些陈述都是有效的。
  • 如果你想要准确,使用===类型检查相等
  • 如果 PHP 的 null 就像 SQL 的 null...

标签: php null comparison mapping operators


【解决方案1】:

我会为您指出几页: 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

【讨论】:

  • 感谢澄清:D,很好奇为什么在 的另一侧有 (int) 时 null 被强制转换为 bool 而不是 (int)
  • @SzymonLukaszczyk - 我也想知道,但第二个链接有一个表“与各种类型的比较”,它在第二行回答了这个问题:如果第一个操作数为空,则操作数都转换为布尔值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 2023-03-23
  • 2023-03-07
  • 2023-03-18
相关资源
最近更新 更多