Underlying philosophy behind php type comparisons

前端 未结 7 1928
不思量自难忘°
不思量自难忘° 2021-02-10 00:38

So there\'s this page on the php site which shows the result of comparing different values:

http://php.net/manual/en/types.comparisons.php

This is a helpful refe

7条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-10 01:04

    There is no particular logic, but you can figure out some patterns.

    • "empty" values (null, false, 0, empty string and string '0') evaluate to false
    • comparison of numeric values is done implicitly converting them to integers until some version (there was a bug when two actually different long numeric strings counted as equal, now it's fixed)
    • when working with arrays, there is no difference between integer and numeric indexes, except when you call array_key_exists with explicit strict parameter
    • comparing number with string implicitly converts right argument to the type of the left one
    • return ($something); implicitly converts $something to string if it is not scalar

提交回复
热议问题