programming.name Posted February 22, 2012 Share Posted February 22, 2012 <?php if('00' == '000') echo 'weired'; ?> Please consider the code above. Strangely it echoes the word "weired". Also if('00' == '000000000000000') returns true and so forth. What is really going on? Thanks in advance By the way the php version is 5.3.9 on wampserver 2.2 Link to comment https://forums.phpfreaks.com/topic/257521-if00-0000-returns-true/ Share on other sites More sharing options...
spiderwell Posted February 22, 2012 Share Posted February 22, 2012 if nothing == nothing then its true! if '000' === '000' if EXACTLY the same then true its like when you have a 0 return for some function which can be seen as false and not 0 so you use 3 equals signs! (i think i didnt explain very well but I hope you get the idea) Link to comment https://forums.phpfreaks.com/topic/257521-if00-0000-returns-true/#findComment-1319918 Share on other sites More sharing options...
requinix Posted February 22, 2012 Share Posted February 22, 2012 For some reason it was decided that if you're loosely comparing two numeric strings, PHP will compare them as numbers. Comparison Operators Quote If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value. As random as spiderwell's comment is, he actually said the right answer: use a strict comparison instead. Link to comment https://forums.phpfreaks.com/topic/257521-if00-0000-returns-true/#findComment-1319921 Share on other sites More sharing options...
xyph Posted February 22, 2012 Share Posted February 22, 2012 Further reading: http://www.greymass.com/php-basics-the-operators-part-1/ Link to comment https://forums.phpfreaks.com/topic/257521-if00-0000-returns-true/#findComment-1319923 Share on other sites More sharing options...
spiderwell Posted February 22, 2012 Share Posted February 22, 2012 Quote As random as spiderwell's comment is, he actually said the right answer: keeping this for signature! Link to comment https://forums.phpfreaks.com/topic/257521-if00-0000-returns-true/#findComment-1319927 Share on other sites More sharing options...
ManiacDan Posted February 22, 2012 Share Posted February 22, 2012 Everyone (except requinix since he's read it three times already) should read this Link to comment https://forums.phpfreaks.com/topic/257521-if00-0000-returns-true/#findComment-1320033 Share on other sites More sharing options...
The Little Guy Posted February 22, 2012 Share Posted February 22, 2012 add a third equal sign <?php if('00' === '000') echo 'weired'; ?> That will not only match 0 against 0 but it must also be of the same type (int, string, bool, float, etc.) it won't convert a string zero to an int. Link to comment https://forums.phpfreaks.com/topic/257521-if00-0000-returns-true/#findComment-1320036 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.