jaikar Posted December 29, 2010 Share Posted December 29, 2010 hi there, is there a difference between if($var == false) and if(false == $var) thankyou! Quote Link to comment https://forums.phpfreaks.com/topic/222939-is-there-a-difference-between-ifvar-false-and-iffalse-var/ Share on other sites More sharing options...
johnny86 Posted December 29, 2010 Share Posted December 29, 2010 No there isn't. It's just comparing. It's the same as asking if this apple is the same color as this orange; or you might as well ask if this orange is the same color as this apple. But there is difference in $var == false and $var === false $var = 0; $var == false // TRUE $var === false // FALSE === will also compare the variable type. It checks if it's a bool, string or integer for example. $var = "1"; $var == 1 // TRUE $var === 1 // FALSE Quote Link to comment https://forums.phpfreaks.com/topic/222939-is-there-a-difference-between-ifvar-false-and-iffalse-var/#findComment-1152728 Share on other sites More sharing options...
Zurev Posted December 29, 2010 Share Posted December 29, 2010 Which is the same as the logical operator ! (NOT) $var = false; if (!$var) { echo "var is equal to false"; } else { echo "var is equal to true"; } Returns var is equal to false. Quote Link to comment https://forums.phpfreaks.com/topic/222939-is-there-a-difference-between-ifvar-false-and-iffalse-var/#findComment-1152734 Share on other sites More sharing options...
the182guy Posted December 29, 2010 Share Posted December 29, 2010 Which is the same as the logical operator ! (NOT) $var = false; if (!$var) { echo "var is equal to false"; } else { echo "var is equal to true"; } Returns var is equal to false. It would be if ($var != true) Quote Link to comment https://forums.phpfreaks.com/topic/222939-is-there-a-difference-between-ifvar-false-and-iffalse-var/#findComment-1152769 Share on other sites More sharing options...
ignace Posted December 30, 2010 Share Posted December 30, 2010 Which is the same as the logical operator ! (NOT) $var = false; if (!$var) { echo "var is equal to false"; } else { echo "var is equal to true"; } Returns var is equal to false. It would be if ($var != true) if(!$var) and if($var != true) or if($var == false) all produce the same result -> "var is equal to false" (not entirely correct it could be aswell: null, '', array(), 0). Junior programmers are often told to write if(false == $var) to make sure they'll never write if(false = $var) or if(1 = $var) as this will produce a fatal error, you can't redefine false nor 1. In case of boolean variables (depending on your companies coding guidelines) you can shorten if($var == false) to if(!$var) as you actually say the same thing: execute when $var = false Quote Link to comment https://forums.phpfreaks.com/topic/222939-is-there-a-difference-between-ifvar-false-and-iffalse-var/#findComment-1152904 Share on other sites More sharing options...
theverychap Posted December 30, 2010 Share Posted December 30, 2010 I'm sure i read somewhere that where comparing like: if ( $var === TRUE ) It's quicker for php to process: if ( TRUE === $var ) An even faster way would be: if ( !$var == TRUE ) And EVEN faster: if ( !empty($var) ) Quote Link to comment https://forums.phpfreaks.com/topic/222939-is-there-a-difference-between-ifvar-false-and-iffalse-var/#findComment-1152907 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.