问题
I am getting a difference when comparing two string which are 0
and '0'
in PHP. Can I do anything to make them be compared equally in an if action?
Thanks!
回答1:
If you compare using:
if ('0' == 0) // if '0' is equal to 0
it should return true as the values are compared with the string being converted to a number. If you do:
if ('0' === 0) // if '0' is identical to 0
it will return false as they have to be of the same type too.
Note the triple '='
回答2:
You can also force their type to be the same before comparing:
if((int)'0' === (int)0) {
// true
}
if((string)'0' === (string)0) {
// true
}
来源:https://stackoverflow.com/questions/2804553/difference-between-numeric-and-string-value-in-php