PHP variable equals true no matter what the value, even 0
Posted
by
kaigoh
on Stack Overflow
See other posts from Stack Overflow
or by kaigoh
Published on 2012-12-17T16:37:04Z
Indexed on
2012/12/17
17:03 UTC
Read the original article
Hit count: 185
This is the var_dump:
object(stdClass)#27 (8) {
["SETTING_ID"]=> string(2) "25"
["SETTING_SOURCE"]=> string(2) "XV"
["SETTING_FLEET"]=> string(3) "313"
["SETTING_EXAM"]=> string(1) "A"
["SETTING_HIDE"]=> string(1) "0"
["SETTING_THRESHOLD"]=> string(1) "0"
["SETTING_COUNT"]=> string(8) "POSITIVE"
["SETTING_USAGE"]=> string(7) "MILEAGE"
}
The variable I am testing is SETTING_HIDE. This is being pulled from MySQL using the Code igniter framework. I don't know if it is just me being thick after a rather long day at work or what, but no matter what value the variable holds, any if statement made against it returns true, even when typecast as a boolean or a direct string comparison, ie. == "0" or == "1".
Anyone with a fresh pair of eyes care to make me feel silly!?! :)
Just to clarify:
Have tried the following:
if($examSetting->SETTING_HIDE == "1") { $showOnABC = "checked=\"checked\""; }
if((bool)$examSetting->SETTING_HIDE) { $showOnABC = "checked=\"checked\""; }
if($examSetting->SETTING_COUNT == "POSITIVE") further on in my code works perfectly.
© Stack Overflow or respective owner