[英]PHP boolean TRUE / FALSE?
I can't figure this out.我想不通。
If I type:如果我输入:
function myfunction(){
......
if ...
return TRUE;
if ...
return FALSE;
}
Why can't I use it like this:为什么我不能这样使用它:
$result = myfunction();
if ($result == TRUE)
...
if ($result == FALSE)
...
Or do I have to use:还是我必须使用:
$result = myfunction();
if ($result == 1)
...
if ($result == 0)
...
Or this:或这个:
$result = myfunction();
if ($result)
...
if (!$result)
...
I don't fully understand your question, but you can use any of the examples you provided, with the following caveats: 我不完全理解您的问题,但您可以使用您提供的任何示例,并注意以下事项:
If you say if (a == TRUE)
(or, since the comparison to true
is redundant, simply if (a)
), you must understand that PHP will evaluate several things as true: 1, 2, 987, "hello", etc.; 如果你说
if (a == TRUE)
(或者,因为与true
的比较是多余的,只是if (a)
),你必须明白PHP将评估几件事情为真:1,2,987,“你好”,等等。; They are all "truey" values. 它们都是“真实”的价值观。 This is rarely an issue, but you should understand it.
这很少是一个问题,但你应该理解它。
However, if the function can return more than true
or false
, you may be interested in using ===
. 但是,如果函数返回的值大于
true
或false
,则可能对使用===
感兴趣。 ===
does compare the type of the variables: "a" == true
is true
, but "a" === true
is false. ===
确实比较变量的类型: "a" == true
为true
,但"a" === true
为false。
If you dont need to use the result variable $result furthermore, I would do the following shortest version: 如果你不需要再使用结果变量$ result,我会做以下最短版本:
if (myfunction()) {
// something when true
} else {
// something else when false
}
You could do like this 你可以这样做
$result = myfunction();
if ($result === TRUE)
...
if ($result === FALSE)
...
您可以使用if($result == TRUE)
但这样就像if($result)
就足够了。
if($variable)
//something when truw
else
//something else when false
Remember that the value -1 is considered TRUE, like any other non-zero (whether negative or positive) number. 请记住,值-1被视为TRUE,与任何其他非零(无论是负数还是正数)一样。 FALSE would be 0 obv...
FALSE将是0 obv ...
Double check you have gettype($result) == bool and NOT string.仔细检查你有 gettype($result) == bool 而不是 string。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.