perikles Posted February 8, 2011 Share Posted February 8, 2011 Hello! Just started getting into PHP! I'd like to set a variable to false, and then check if it is set to false ( set to false = true ), but I'm confused about assignment operators vs. comparison operators, no pun intended. = false or == false ? Link to comment https://forums.phpfreaks.com/topic/227010-confirming-false/ Share on other sites More sharing options...
Rifts Posted February 8, 2011 Share Posted February 8, 2011 $awesomeVar = false; if ( $awesomeVar == false) { echo "its FALSE"; } else { ehco "its NOT false"; } Link to comment https://forums.phpfreaks.com/topic/227010-confirming-false/#findComment-1171225 Share on other sites More sharing options...
btherl Posted February 8, 2011 Share Posted February 8, 2011 If you want to check if a variable is really false, and not anything else, you should do this: if ($var === false) If you want to check if a variable is false or null or 0 or empty string, use this: if ($var == false) If you want to set a variable to false: $var = false; So to set and do a strict check: $var = false; if ($var === false) { print "False is true."; } Link to comment https://forums.phpfreaks.com/topic/227010-confirming-false/#findComment-1171226 Share on other sites More sharing options...
perikles Posted February 8, 2011 Author Share Posted February 8, 2011 Excellent, thanks to you both! Link to comment https://forums.phpfreaks.com/topic/227010-confirming-false/#findComment-1171228 Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2011 Share Posted February 8, 2011 Rifts, the problem with that is if the value of $awesomeVar is 0 (zero), it will evaluate to true. To check for boolean FALSE, you need to use the identical comparison === operator, not the equals comparison == operator. if( $var === FALSE ) Link to comment https://forums.phpfreaks.com/topic/227010-confirming-false/#findComment-1171231 Share on other sites More sharing options...
perikles Posted February 8, 2011 Author Share Posted February 8, 2011 Did not really look at the identity operator before -- very interesting. That's what I need! Link to comment https://forums.phpfreaks.com/topic/227010-confirming-false/#findComment-1171233 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.