开发者

Difference between numeric and string value in PHP

开发者 https://www.devze.com 2022-12-30 00:06 出处:网络
I am getting a difference when comparing two string which are 0 and \'0\' in PHP开发者_JS百科. Can I do anything to make them be compared equally in an if action?

I am getting a difference when comparing two string which are 0 and '0' in PHP开发者_JS百科. Can I do anything to make them be compared equally in an if action?

Thanks!


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


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号