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?)
You are comparing your variable (that contains
(bool) true/(bool) false) toboolean. The Simple literalbooleanis 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) truewhich returnstrueand(bool) false == (bool) truewhich returnsfalseofcourse.