help with returning boolean true or false from functions
Posted: Fri Mar 06, 2009 10:16 pm
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:
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:
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?
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";
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?