Forum Moderators: coopster
function foo(var, var2)
{
if(!var &&!var2)
return A;
else
return B;
}
Now, if I use the following:
foo(false, false);
it returns A (as it should)
but if I use
foo(0, 0);
It returns B and I can't figure out why.
Anyone have any insights?
This should work as well:
function foo( (bool) $var, (bool) $var2 )
{
if(!$var &&!$var2)
return A;
else
return B;
}
Actually, the above DOES NOT WORK! Apparently, you cannot type cast in the argument section of a function.
This one DOES WORK:
function foo( $var, $var2 )
{
if(!(bool)$var &&!(bool)$var2)
return A;
else
return B;
}
[edited by: Birdman at 3:39 pm (utc) on Sep. 30, 2006]
Also, you are using undefined constants. Turn up error_reporting() [php.net] during development so you can find these issues. Working example ...
function foo($var, $var2)
{
if (!$var && !$var2) {
return "A";
} else {
return "B";
}
}
print foo(false, false);
print foo(0, 0);
exit;
It turns out that the following returns A:
$var = "foo";
$var2 = 0;
if($var == $var2)
return A;
else
return B;
That pretty much was the crux of the issue with the function I was making, I assumed that a value (any value that wasn't null, false or 0) in $var would result in $var being true, turns out PHP doesn't agree with me.
$var = "foo";
$var2 = 0;
var_dump($var == $var2);
// prints bool(true)