PHP intval() weird results

前端 未结 5 1155
无人及你
无人及你 2020-12-20 06:26

I\'m encountering something weird and I don\'t know why it is happening!

I have a URL like:

http://mysite.com/users/USER_ID

this user id

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 07:03

    PHP is a little odd when it comes to types.

    Basically, what you are doing is parsing the string into a number (so 'abcdef' returns 0 because it isn't a number at all), then comparing the original string to the number.

    Now, I can see why you would assume that it should be false, but PHP tries to be clever. Basically, == will coerce types, and almost always coerces to numbers if one of it's values is a number. So it is using the same conversion that you did on the string, then comparing.

    It is a better idea to use === which also checks types.

提交回复
热议问题