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)
...
Value 0 and 1 is equal to false and true in php.
Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true. To make life easier, C Programmers typically define the terms "true" and "false" to have values 1 and 0 respectively.
string – “0” and null string are false and everything else is true (even “0.0”) array – empty array is false and everything else is true. object – here null is false and everything else is true. null – null is always false.
Boolean values and operationsConstant true is 1 and constant false is 0. It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0.
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.; 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 ===
. ===
does compare the type of the variables: "a" == true
is true
, but "a" === true
is false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With