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? Link to comment https://forums.phpfreaks.com/topic/60338-whats-the-difference-between-and/ 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 } ?> Link to comment https://forums.phpfreaks.com/topic/60338-whats-the-difference-between-and/#findComment-300190 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 ==. Link to comment https://forums.phpfreaks.com/topic/60338-whats-the-difference-between-and/#findComment-300193 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. Link to comment https://forums.phpfreaks.com/topic/60338-whats-the-difference-between-and/#findComment-300194 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. Link to comment https://forums.phpfreaks.com/topic/60338-whats-the-difference-between-and/#findComment-300216 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.