Underlying philosophy behind php type comparisons

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

    The base pattern is the same to the one used in C: anything non-zero is true for the sake of boolean comparisons.

    In this sense, an empty string or array is also false.

    The hairy scalar to look out for is '0', which is (very inconveniently) treated as empty too because it gets converted to an integer. array(0) is just as thorny on the array front.

    When using strict comparisons (=== and !==), things are a lot more sane. In practice, it's often a good idea to cast input coming from superglobals and the database as appropriate, and to use these operators from that point forward.

提交回复
热议问题