Page 1 of 1

help with returning boolean true or false from functions

Posted: Fri Mar 06, 2009 10:16 pm
by catatungman
I'm new to PHP but have a good understanding of programming from other languages. I'm more curious than anything about this "problem"... If anyone can explain to me why it acts the way it does it would cure my curiousity and I might learn something too.

Here is some code:

Code: Select all

 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
    <?PHP
        function brown ($a) {
            echo "function was initiated with \"{$a}\" sent.";
            echo "<br />";
            return true;
        }
 
$result = brown("hut");
echo "result = {$result}";
echo "<br />";
if ($result=="true") echo "boolean of result tested true<BR/>";
if ($result=="false") echo "boolean of result tested false";
?>
 
<body>
</body>
</html>
 
 

When I run this as it is shown, my output is:

-----
function was initiated with "hut" sent.
result = 1
boolean of result tested true
boolean of result tested false
-----


It shows the value was passed on to the function.
It shows "result" has a value.
however, the two IF statements that test the very same variable for two different things both execute... how is that possible?? How could it be true AND false?

Just to test the theory of the result being passed, I've changed the "return true;" to "return false;" in the function.
The output is:

-----
function was initiated with "hut" sent.
result =
-----

It shows the value was passed on to the function.
It shows result has no value this time, which means there is a difference between the return true and return false.
this leads me to believe it IS using a boolean... where 1 is true and nothing (null) is false.

however in this situation with false being returned, NEITHER of the IF statements execute.

Does anyone understand what is going on? The only thing I can think of is if it's a result of testing a non-boolean variable for boolean status, that is, the IF statements are actually checking if ANY value exists somehow. But it doesn't make sense 100% because I would think "true" and "false" would account for different scenerios and both would not run, under any circumstances, whether it was testing for a strings content or a boolean expression.

I've gotten this to work by doing:

Code: Select all

 
if ($result=="true") echo "boolean of result tested true<BR/>";
else echo "boolean of result tested false";
 
So I am not actually stuck but I would like to understand how the computer is thinking
and if I am doing this in a less efficient way so I can learn from this and become better
at programming PHP...

Anyone understand whats going on?

Re: help with returning boolean true or false from functions

Posted: Fri Mar 06, 2009 10:26 pm
by susrisha

Code: Select all

 
if ($result=="true") echo "boolean of result tested true<BR/>";
if ($result=="false") echo "boolean of result tested false";
 
You are checking the result with two strings not the boolean values.
Hence in the first case, it just checks if the string has any value and returns a true.

Code: Select all

 
if ($result==true) echo "boolean of result tested true<BR/>";
if ($result==false) echo "boolean of result tested false";
//i think this is the right syntax for you to check
 

Re: help with returning boolean true or false from functions

Posted: Fri Mar 06, 2009 10:29 pm
by atonalpanic
Try using the identity operator (===) instead of the equality operator (==).

EDIT: Someone already answered and saw a part I missed...

Re: help with returning boolean true or false from functions

Posted: Fri Mar 06, 2009 10:33 pm
by catatungman
ah, you are correct, that solved the problem! I understand it a little better now.

I had actually thought of that at one point and tried not using quotes... But that was before when I was trying to test on a single IF line (eg.

Code: Select all

if (brown("hut"))==true echo "true")
but it didn't work as I'm not sure if you can check the boolean response from a function without assigning it to a variable first. So I broke it down with a variable and didn't try without
the quotes after I made that change.

Do you know, can you test a function's result boolean status in an IF statement, without assigning it to a variable first?

Thanks for the reply!

Randy

Re: help with returning boolean true or false from functions

Posted: Fri Mar 06, 2009 10:43 pm
by susrisha

Code: Select all

 
if (brown("hut"))==true echo "true");//thats a parsing error 
 

Code: Select all

 
if (brown("hut")==true) 
{
   echo "true";
}
//consider using this..
 

Code: Select all

 
if (brown("hut"))
{
echo "true";
}
//this is still a better way