views:

71

answers:

2

Hi everyone,

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!

+10  A: 

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 '='

Gazler
+1  A: 

You can also force their type to be the same before comparing:

if((int)'0' === (int)0) {
    // true
}
if((string)'0' === (string)0) {
    // true
}
nuqqsa