nloding Posted September 11, 2008 Share Posted September 11, 2008 I'm a little confused. I see this more and more often when I look at open-source apps: if(''===$var) In every article, every book, that I have ever read (not that I've read nearly everything or that everything I've read was good), it's always been this: if($var=='') What's the difference between '===' and '==', and why put the value first instead of the variable? Is there a performance or accuracy difference? Is '===' case-sensitive? How can it be more strict than '=='? I Googled it, but some of the things I read made me more confused ... Quote Link to comment https://forums.phpfreaks.com/topic/123829-solved-help-understanding-vs/ Share on other sites More sharing options...
DeanWhitehouse Posted September 11, 2008 Share Posted September 11, 2008 This will help http://uk3.php.net/manual/en/language.operators.comparison.php PHP Manual: $a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4) Quote Link to comment https://forums.phpfreaks.com/topic/123829-solved-help-understanding-vs/#findComment-639339 Share on other sites More sharing options...
nloding Posted September 11, 2008 Author Share Posted September 11, 2008 OK, so they are of the same type, that makes sense. Is there anything to putting the value or the variable first during comparison? Quote Link to comment https://forums.phpfreaks.com/topic/123829-solved-help-understanding-vs/#findComment-639341 Share on other sites More sharing options...
DeanWhitehouse Posted September 11, 2008 Share Posted September 11, 2008 No putting the var after, wouldnt be the same as putting it before. e.g. if($var != '') is not the same as if('' != $var) As they will return different results , so i think. Quote Link to comment https://forums.phpfreaks.com/topic/123829-solved-help-understanding-vs/#findComment-639343 Share on other sites More sharing options...
Mchl Posted September 11, 2008 Share Posted September 11, 2008 Blade280891: both evaluate same here There shouldn't be anything to it, as far as I know. I think it's just personal preference. Quote Link to comment https://forums.phpfreaks.com/topic/123829-solved-help-understanding-vs/#findComment-639346 Share on other sites More sharing options...
DeanWhitehouse Posted September 11, 2008 Share Posted September 11, 2008 If you look at it logically , you are saying if variable one doesnt equal empty , then do if($var != '') or if empty doesnt equal variable one , then do if('' != $var) Quote Link to comment https://forums.phpfreaks.com/topic/123829-solved-help-understanding-vs/#findComment-639348 Share on other sites More sharing options...
Mchl Posted September 11, 2008 Share Posted September 11, 2008 Which is exactly the same (at least in boolean logic - lawyers might have different opinion ) Quote Link to comment https://forums.phpfreaks.com/topic/123829-solved-help-understanding-vs/#findComment-639353 Share on other sites More sharing options...
nloding Posted September 11, 2008 Author Share Posted September 11, 2008 If you look at it logically , you are saying if variable one doesnt equal empty , then do if($var != '') or if empty doesnt equal variable one , then do if('' != $var) Yeah, but logically, to me, those end up with the same result. Either they are equal or they aren't. 2 * 3 = 6, and 6 = 3 * 2. I was just curious if there was performance difference or something, but it sounds like I'll have to bench it to find out. Quote Link to comment https://forums.phpfreaks.com/topic/123829-solved-help-understanding-vs/#findComment-639354 Share on other sites More sharing options...
448191 Posted September 11, 2008 Share Posted September 11, 2008 Of course that's the same. The primary motivation for people placing the value in the comparison on the left side is that it makes in impossible to accidentally use assignment instead of comparison. Quote Link to comment https://forums.phpfreaks.com/topic/123829-solved-help-understanding-vs/#findComment-639356 Share on other sites More sharing options...
Mchl Posted September 11, 2008 Share Posted September 11, 2008 !! That makes sense! Quote Link to comment https://forums.phpfreaks.com/topic/123829-solved-help-understanding-vs/#findComment-639357 Share on other sites More sharing options...
nloding Posted September 11, 2008 Author Share Posted September 11, 2008 makes in impossible to accidentally use assignment instead of comparison. Never would have occurred to me. That's a great idea though. Quote Link to comment https://forums.phpfreaks.com/topic/123829-solved-help-understanding-vs/#findComment-639358 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.