我有一個函數返回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