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 ? Quote Link to comment 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"; } Quote Link to comment 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."; } Quote Link to comment Share on other sites More sharing options...
perikles Posted February 8, 2011 Author Share Posted February 8, 2011 Excellent, thanks to you both! Quote Link to comment 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 ) Quote Link to comment 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.