Forum Moderators: coopster

Message Too Old, No Replies

false and 0 not the same?

What's going on in my functions calls?

         

Steerpike

1:37 am on Sep 30, 2006 (gmt 0)

10+ Year Member




I have a function that takes two arguments, looks a bit like this:

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?

DrDoc

1:56 am on Sep 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
$a = null;
if($a) echo "a\n";
$b = false;
if($b) echo "b\n";
$c = 0;
if($c) echo "c\n";
$d = -1;
if($d) echo "d\n";
$e = 1;
if($e) echo "e\n";
$f = "";
if($f) echo "f\n";
if($a == $b) echo "a=b\n";
if($a == $c) echo "a=c\n";
if($a == $d) echo "a=d\n";
if($a == $e) echo "a=e\n";
if($a == $f) echo "a=f\n";
if($b == $c) echo "b=c\n";
if($b == $d) echo "b=d\n";
if($b == $e) echo "b=e\n";
if($b == $f) echo "b=f\n";
if($c == $d) echo "c=d\n";
if($c == $e) echo "c=e\n";
if($c == $f) echo "c=f\n";
if($d == $e) echo "d=e\n";
if($d == $f) echo "d=f\n";
if($e == $f) echo "e=f\n";
?>


d
e
a=b
a=c
a=f
b=c
b=f
c=f

DrDoc

1:59 am on Sep 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, in other words:

null = false
null = 0
null = ""
false = 0
false = ""
0 = ""

Psychopsia

3:04 am on Sep 30, 2006 (gmt 0)

10+ Year Member



function foo($var, $var2)
{
if($var === false && $var2 === false)
return A;
else
return B;
}

Birdman

3:26 pm on Sep 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note: the example in the previous post behaves exactly the same as the original poster's example.

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]

coopster

11:40 am on Oct 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The original function will work as designed, once you clean up the syntax errors. First, the function is defined with invalid variables names. So you need to have the dollar sign ($) in front of your var and var2 in the function definition default argument list and also inside the function where you refer to them in your if control statement.

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;

Steerpike

11:10 pm on Oct 2, 2006 (gmt 0)

10+ Year Member



My original function didn't explain it very well, but I've figured out the issue.

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.

Jordo needs a drink

2:25 am on Oct 3, 2006 (gmt 0)

10+ Year Member



Well that confused me. How can that return A? I have similar problems trying to figure out what is true and what is false.

coopster

2:34 am on Oct 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



They are different types so PHP converts prior to comparison [php.net].
$var = "foo"; 
$var2 = 0;
var_dump($var == $var2);
// prints bool(true)

Jordo needs a drink

5:07 am on Oct 3, 2006 (gmt 0)

10+ Year Member



got it. thanx