我有一个函数返回TRUE
或FALSE
或"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.
你并没有真正“解决”问题,避免布尔规避它尝试。虽然在这种情况下你的解决方案可以工作,但字符串的平等可能与字符串一样棘手。我真的建议在做比较的时候,使用===来完全确定类型和值是比较的。哦,请不要忘记投票给出最好的答案,你的帖子的接受评级是低的。感谢您和美好的一天。 – stefgosselin
@stefgosselin ohh对不起,我接受了你的答案[第一个答案和好解释],它看起来像PHP是非常糟糕的,当我们比较C#,但它很容易使用。 – Red
我将我的代码更改为'===' – Red