Jump to content

if('00' == '0000') returns true


programming.name

Recommended Posts

 

<?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

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)

For some reason it was decided that if you're loosely comparing two numeric strings, PHP will compare them as numbers.

 

Comparison Operators

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.