[英]PHP boolean TRUE / FALSE?
我想不通。
如果我輸入:
function myfunction(){
......
if ...
return TRUE;
if ...
return FALSE;
}
為什么我不能這樣使用它:
$result = myfunction();
if ($result == TRUE)
...
if ($result == FALSE)
...
還是我必須使用:
$result = myfunction();
if ($result == 1)
...
if ($result == 0)
...
或這個:
$result = myfunction();
if ($result)
...
if (!$result)
...
我不完全理解您的問題,但您可以使用您提供的任何示例,並注意以下事項:
如果你說if (a == TRUE)
(或者,因為與true
的比較是多余的,只是if (a)
),你必須明白PHP將評估幾件事情為真:1,2,987,“你好”,等等。; 它們都是“真實”的價值觀。 這很少是一個問題,但你應該理解它。
但是,如果函數返回的值大於true
或false
,則可能對使用===
感興趣。 ===
確實比較變量的類型: "a" == true
為true
,但"a" === true
為false。
如果你不需要再使用結果變量$ result,我會做以下最短版本:
if (myfunction()) {
// something when true
} else {
// something else when false
}
你可以這樣做
$result = myfunction();
if ($result === TRUE)
...
if ($result === FALSE)
...
您可以使用if($result == TRUE)
但這樣就像if($result)
就足夠了。
if($variable)
//something when truw
else
//something else when false
請記住,值-1被視為TRUE,與任何其他非零(無論是負數還是正數)一樣。 FALSE將是0 obv ...
仔細檢查你有 gettype($result) == bool 而不是 string。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.