tags:

views:

231

answers:

6

i cant figure this out.

if i type

function myfunction(){
    ......
    if ...
        return TRUE;
    if ...
        return FALSE;
}

why cant 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)
...
A: 

for safety, you should use the last one.

$result = myfunction();
if ($result)
...
if (!$result)
...
Shivan Raptor
except that 0, '0', '' and a collection of others would also evaluate as false using this shorthand, and that is not always desirable...
HorusKol
A: 
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. FALSE would be 0 obv...

pcp
+2  A: 

You could do like this

$result = myfunction();
if ($result === TRUE)
...
if ($result === FALSE)
...
S.Mark
i'd say you should do it like this...
HorusKol
A: 

Maybe this article will help you out.

Shiv
+1  A: 

You can use if($result == TRUE) but that's an overkill as if($result) is enough.

Amarghosh
+1  A: 

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.

Tordek