2011-11-24 89 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