if ($fromcat == "true" || $fromcat == 1){
error_log("binding to CAT " . $cat . " because pfc was " . $fromcat);
} else {
error_log("binding to ROOM " . $room . " because pfc was " . $fromcat);
}
请查看上面的代码。
我想每个人都同意,如果$fromcat
为0,我无法在控制台中获得“绑定到CAT(...)”消息。
然而,这是:
[01-Apr-2013 22:34:50] binding to CAT single because pfc was 0
怎么可能呢?您不能同时显示单词CAT和数字0! PHP喝醉了吗?
答案 0 :(得分:3)
使用相同的(===
)比较运算符。
if ($fromcat === "true" || $fromcat === 1) {
error_log("binding to CAT " . $cat . " because pfc was " . $fromcat);
} else {
error_log("binding to ROOM " . $room . " because pfc was " . $fromcat);
}
您正在尝试将$fromcat
与字符串"true"
进行比较。如果你想将它与布尔值true进行比较,你可以使用$fromcat == true
。否则:
如果您将数字与字符串进行比较或比较涉及数字字符串,则每个字符串都会转换为数字,并且数字会进行比较。
答案 1 :(得分:1)
逆转你的病情顺序也是一个好主意。
所以
if ($fromcat === "true" || $fromcat === 1)
变为
if ('true' === $fromcat || 1 === $fromcat)