0
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喝醉了吗?

4

2 回答 2

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); 

}

您正在尝试与$fromcatstring进行比较"true"。如果您想将其与布尔值 true 进行比较,您将使用$fromcat == true. 除此以外:

如果将数字与字符串进行比较或比较涉及数字字符串,则每个字符串都将转换为数字并以数字方式执行比较。

文档

于 2013-04-01T20:41:46.640 回答
1

颠倒您的病情顺序也是一个好主意。

所以

if ($fromcat === "true" || $fromcat === 1)

变成

if ('true' === $fromcat || 1 === $fromcat)

原因:PHP - if 语句中的颠倒顺序

于 2013-04-01T20:53:52.013 回答