[PHP] $VAR = 0 != false From: Brian T. Allen (brian <email protected>)
Date: 08/07/00

Hi *,

I have an ecommerce site where the shopping cart supports options for items.

One of the items has an option that is stored in the database as 0 (zero). It doesn't mean false, it means that an option was selected, and the option was 0.

If I echo out the variable, you get "0" echoed out.

$VAR = "0";
echo $VAR;

You get what you would expect.

But, if you verify the variable exists first:

if($VAR){
    echo $VAR;
}

You get nothing, even though the variable is set. It is set to zero, granted, but it is set!

I can understand that in some types of tests you want 0 to be synonimous with false, but here I am only testing if the variable is set to a value, be it false, 0, or something else.

I just tested, and isset() works like I think it should (I am NOT the source of all truth and knowledge, just trying to make this make sense), but !empty() does not:

if(!empty($VAR)){
    echo $VAR;
}

even though $VAR isn't empty.

Thoughts, comments, flames?

Thanks,
Brian