Underlying philosophy behind php type comparisons

前端 未结 7 1816
不思量自难忘°
不思量自难忘° 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:03

    There is imo a very straightforward guideline and a bug in the specification, which might be confusing.

    Strict comparison checks equality in datatype and value. Loose comparison checks equality in value only.

    For an object (not part of the comparison table) is php quite straightforward: if the object is the same instance as the other one, then is it strictly equal, otherwise might it be loosely equal.

    Therefor is a 0 and a "0" loosely equal to each other and to false (and to any string). The latter can be understood as all strings are not numeric, hence false and the number that is equal to false is 0, hence all strings are equal to 0.

    The comparison between null and array() is more complicated. If you check an array created with array() and compare that loosely and strictly, then will it return true. If you however check it with is_null, then will it return false. I think the latter is more logical, because an array() created with array() is not equal to '', where null is. I would think that this functional inconsistency between the function is_null() and the checks == null or === null a bug, because it should not happen that using two different valid methods to check for a value return different results. Null is also not an array according to the function is_array(), which is true. An empty array is an array according to the function is_array(), which should be true too. Hence should it never be true that null is equal to array().

提交回复
热议问题