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

后端 未结 5 1376
南方客
南方客 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:13

    pretty much any non-zero value gets converted to true in php behind the scenes.

    so 1, 2,3,4, 'Hello', 'world', etc would all be equal to true, whereas 0 is equal to false

    the only reason !== works is cause it is comparing data types are the same too

提交回复
热议问题