user849137
user849137

Reputation:

failing to define an array key with a value of false

So I have a multi-dim array. I'm defining a few array keys with boolean as values:

$geo_entries['hostip']['geoTypes']['country']=true;
$geo_entries['hostip']['geoTypes']['country_short']=true;
$geo_entries['hostip']['geoTypes']['city']=false;
$geo_entries['hostip']['geoTypes']['city_short']=false;

Now I do a print_r() on that, and this is the result:

( [country] => 1 [country_short] => 1 [city] => [city_short] => )

Now correct me if I'm wrong, but doesn't false==0?

I try to do a quick check on the value (for booleans):

if($geo_entries['hostip']['geoTypes']['city']==boolean){
//this returns false...
}

The condition above returns false with ['hostip']['geoTypes']['city'] but true with ['hostip']['geoTypes']['country']. The only difference between the two is that city has the value of false and country has the value of true.

When I define the value as 0 instead of false - all works well...

I have a feeling there is something I have embarrassingly missed, which is resulting to this misunderstanding.

Anyone care to explain? - (Why false!=0?)

Upvotes: 1

Views: 2133

Answers (5)

Kep
Kep

Reputation: 5857

You are comparing your variable (that contains (bool) true / (bool) false) to boolean. The Simple literal boolean is not defined, PHP handles it as a string.

if($geo_entries['hostip']['geoTypes']['city']==boolean)

therefore becomes

if($geo_entries['hostip']['geoTypes']['city']=="boolean")

The == operator compares those to after type-juggling. "boolean" is a non-empty string and gets treated as (bool) true. So your comparisions boil down to (bool) true == (bool) true which returns true and (bool) false == (bool) true which returns false ofcourse.

Upvotes: 3

Benjamin Cox
Benjamin Cox

Reputation: 6120

False is not equal to 0 because the type of the variable you're checking is boolean.

http://us.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting

The doc says the following values are false:

  • the boolean FALSE itself
  • the integer 0 (zero)
  • ...

This does NOT say that boolean FALSE == integer 0.

Upvotes: 0

Will Vousden
Will Vousden

Reputation: 33388

This is not how you do type comparison in PHP. If you want to check whether a variable $foo is a boolean, you can do this:

if (is_bool($foo))
{
    // ...
}

What your example is in fact doing is interpreting boolean as a string and checking whether it should be considered true when interpreted as a boolean value. This will generate an E_NOTICE message (which may or may not be visible depending on your error reporting level).

Upvotes: 0

billyonecan
billyonecan

Reputation: 20270

foreach($geo_entries['hostip']['geoTypes'] as $key => $value) {
  echo $key . ' is ' . (is_bool($value) ? 'boolean' : 'not boolean') . '<br />'; 
}

outputs:

country is boolean
country_short is boolean
city is boolean
city_short is boolean

Upvotes: 0

earth_tom
earth_tom

Reputation: 831

You can verify that the issue is with the printing via print_r and not the setting. The following line will output bool(false)

var_dump( $geo_entries['hostip']['geoTypes']['city']);

also var_dump( $geo_entries['hostip']['geoTypes']['city'] == false); will output bool(true)

and var_dump( $geo_entries['hostip']['geoTypes']['city'] == 0); will output bool(true)

Slightly off-topic: Generally, it's a good idea to avoid treating boolean values to integers to make the code more readable, especially for developers who use multiple languages.

Upvotes: 0

Related Questions