Why does (0 == 'Hello') return true in PHP?

后端 未结 5 1379
南方客
南方客 2020-12-03 16:53

Hey, if you have got the following code and want to check if $key matches Hello I\'ve found out, that the comparison always returns true

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 17:16

    The operators == and != do not compare the type. Therefore PHP automatically converts 'Hello' to an integer which is 0 (intval('Hello')). When not sure about the type, use the type-comparing operators === and !==. Or better be sure which type you handle at any point in your program.

提交回复
热议问题