jwhite68 Posted July 17, 2007 Share Posted July 17, 2007 Until recently, I had understood that !== and != were the same. However, while trying to solve a bug, I found that the problem was checking !== instead of !=. Can anyone throw any light on this subject? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 17, 2007 Share Posted July 17, 2007 The equality comparison (==) will return true if two variables are equal. The identical comparison (===) will return true if the variables are equal AND of the same type. <?php if(0=='0'){ //true } if (0 === '0'){ //false } ?> Quote Link to comment Share on other sites More sharing options...
NArc0t1c Posted July 17, 2007 Share Posted July 17, 2007 I guess PHP handles != as if was declaring a variable. The ! in front is probably not used as it picks up the = and not two ==. Quote Link to comment Share on other sites More sharing options...
Lumio Posted July 17, 2007 Share Posted July 17, 2007 Hi !== and != is the converse of === and == So I give you an example: You got two variables One with the value true and one with the value 1: Now the first request with != <?php $var1 = true; $var2 = 1; if ($var1 != true) echo 'The value of $var1 is false'; else echo 'The value of $var1 is true'; if ($var2 != true) echo 'The value of $var2 is false'; else echo 'The value of $var2 is true'; ?> Now the same with !== <?php $var1 = true; $var2 = 1; if ($var1 !== true) echo 'The value of $var1 is false'; else echo 'The value of $var1 is true'; if ($var2 !== true) echo 'The value of $var2 is false'; else echo 'The value of $var2 is true'; ?> See the different? So != controlls if the two values are the same but don't look after type of the two values. !== controlls both... type of the variable and the values itself. Quote Link to comment Share on other sites More sharing options...
jwhite68 Posted July 17, 2007 Author Share Posted July 17, 2007 Hmm. I think I understand. In my case, I was testing whether a variable (that could be assigned to a date field from a $_REQUEST['date']) was a null string or not. So I am trying to understand whether its best practise to always use != for such scenarios. Since !== doesnt work in this comparison. 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.