2011-11-24 90 views
1

我有一个函数返回TRUEFALSE"Test_operation",我正在循环它做一些事情。正如你所看到的$comment_reply的值是Test_operation字符串值等于true

$comment_reply = $this->Profunction->insert_comments(); 
echo $comment_reply; // returns Test_operation 
if ($comment_reply==TRUE) 
{ 
    echo json_encode('Success'); 
} 
elseif($comment_reply==FALSE) 
{ 
    echo json_encode('Failed'); 
} 
elseif($comment_reply =="test_operation") 
{ 
    echo json_encode('This Action Cannot be done!'); 
} 

但还是

if ($comment_reply==TRUE) 
{ 
    echo json_encode('Success'); 
} 

这部分得到执行。为什么会发生?

在此功能,我回来是这样的:

return TRUE;  // if all success 
return FALSE;  // if there is some problems 
return "Test_operation";  //No insertion need to be done,may be for preview purpose. 

解决:我改变bool值的字符串。 因此,这将是

return 'TRUE';  // if all success 
    return 'FALSE';  // if there is some problems 
    return "Test_operation";  //No insertion need to be done,may be for preview purpose. 
+0

你并没有真正“解决”问题,避免布尔规避它尝试。虽然在这种情况下你的解决方案可以工作,但字符串的平等可能与字符串一样棘手。我真的建议在做比较的时候,使用===来完全确定类型和值是比较的。哦,请不要忘记投票给出最好的答案,你的帖子的接受评级是低的。感谢您和美好的一天。 – stefgosselin

+0

@stefgosselin ohh对不起,我接受了你的答案[第一个答案和好解释],它看起来像PHP是非常糟糕的,当我们比较C#,但它很容易使用。 – Red

+0

我将我的代码更改为'===' – Red

回答

3

不知道这是你的问题,但如果你想通过类型强制平等值,使用===操作。

您可以在comparison operator page上查看更详细的信息。

快乐编码

+0

谢谢,但我认为如果我避免使用'boolean',我可以避免这种情况。 – Red

+0

如你所愿,我的朋友。布尔的没关系,你只需要在PHP中记住很多东西== true,而===只会像你期望的那样返回true。 – stefgosselin

+0

我现在明白了,实际上我是来自c#并尝试使用PHP,这就是我的问题。 – Red